Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql How to only select from a column if the column exists

I need to be able to check if a column exists and if it does then I want to SELECT from it.

I am trying lots of different variations but I'm not even sure if this is possible.

Here's my latest attempt:

SELECT
IF (EXISTS (SELECT `Period` AS `Period` FROM myview), `PERIOD`,
IF (EXISTS (SELECT `Country` AS `COUNTRY` FROM myview),`COUNTRY` FROM myview ;

Any ideas?


EDIT


I had seen the other question on here: MySQL, Check if a column exists in a table with SQL

But I still struggle with the if statement. I can check to see if the column exists using the answer in the question above. But my question is - how to execute a select statement from that column if the result is found to be true.


EDIT 2


The answer below indicates that I should use the BEGIN and END statement and this makes sense. However, my query complains at the first line. It says 'unexpected IF' - can anybody confirm if this is the right syntax fro MYSQL?

if( exists (SELECT * 
    FROM information_schema.COLUMNS 
    WHERE TABLE_SCHEMA = 'db_name' 
    AND TABLE_NAME = 'view_name' 
    AND COLUMN_NAME = 'column_name') )
begin
    SELECT `column_name` FROM `view_name`
end

Thanks in advance.

like image 813
Linda Keating Avatar asked Jun 12 '14 21:06

Linda Keating


People also ask

Is it possible to conditionally create column in MySQL?

Is this really possible to conditionally create Column ? MySQL ALTER TABLE does not have IF EXISTS specification. You can do the following through using a stored proc or a programming language if this is something that you'll need to do on a regular basis: SELECT column_name FROM INFORMATION_SCHEMA.

How to select from another column if selected value is '0' in MySQL?

Select from another column if selected value is '0' in MySQL? Select from another column if selected value is '0' in MySQL? For this, use IF () in MySQL. The syntax is as follows −

How do I find if a column exists in a table?

Find if the column exists using the SQL below: SELECT column_name FROM INFORMATION_SCHEMA. COLUMNS WHERE TABLE_SCHEMA = [Database Name] AND TABLE_NAME =

How to use if exists in MySQL ALTER TABLE?

MySQL ALTER TABLE does not have IF EXISTS specification. You can do the following through using a stored proc or a programming language if this is something that you'll need to do on a regular basis: SELECT column_name FROM INFORMATION_SCHEMA.


2 Answers

This query will give you whether a column exists.

SELECT * 
FROM information_schema.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'db_name' 
AND TABLE_NAME = 'table_name' 
AND COLUMN_NAME = 'column_name'

If you want to check if some columns exist then perform a select statement you need to first check your columns exist. Then perform the select:

if (exists (SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'myview' AND COLUMN_NAME = 'Period') and exists (SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'myview' AND COLUMN_NAME = 'Country'))
begin
    select `Period`, `Country` from myview
end

If the IF condition is true, then you will execute anything inside the BEGIN and END.

like image 57
James McDonnell Avatar answered Oct 17 '22 15:10

James McDonnell


I came across the same situation where I had some product tables created by sheets uploaded by users. Sometimes, the sheets did not have column named "obsolete", so I had to import all products from the sheet but not the obsolete ones.

I am not modifying my query based on the original question that was asked, but here is my solution:

SELECT
    t2.model,
    (
        SELECT
            COUNT(*) AS _count
        FROM db.table t1
        WHERE
            `obsolete`=1
            AND t1.model=t2.model
    ) AS `obsolete`
FROM (
    SELECT
        0 AS `obsolete`,
        t3.model
    FROM db.table t3
) t2

There are 2 most important parts in this query:

  1. We are selecting 0 AS obsolete as dummy to fool MySql which will be used even if column does not exist when selecting COUNT(*).
  2. We have named tables as t1 & t2 to match the column model as t1.model=t2.model.
like image 1
Rehmat Avatar answered Oct 17 '22 16:10

Rehmat