我需要能够检查列是否存在,如果存在,那么我想从中进行SELECT。
我正在尝试很多不同的变化,但我甚至不确定这是否可行。
这是我最近的尝试:
SELECT IF (EXISTS (SELECT `Period` AS `Period` FROM myview), `PERIOD`, IF (EXISTS (SELECT `Country` AS `COUNTRY` FROM myview),`COUNTRY` FROM myview ;有任何想法吗?
编辑
我在这里看到了另一个问题: MySQL,检查SQL中是否存在列
但我仍然对if语句感到困惑。 我可以使用上面问题中的答案检查列是否存在。 但我的问题是 - 如果发现结果为真,如何从该列执行select语句。
编辑2
下面的答案表明我应该使用BEGIN和END语句,这是有道理的。 但是,我的查询在第一行抱怨。 它说'意外的IF' - 任何人都可以确认这是否是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提前致谢。
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` endThanks in advance.
最满意答案
此查询将为您提供列是否存在。
SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'db_name' AND TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name'如果要检查是否存在某些列,则执行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条件为真,那么您将执行BEGIN和END内的任何内容。
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 endIf the IF condition is true, then you will execute anything inside the BEGIN and END.
Mysql如果列存在,如何仅从列中进行选择(Mysql How to only select from a column if the column exists)我需要能够检查列是否存在,如果存在,那么我想从中进行SELECT。
我正在尝试很多不同的变化,但我甚至不确定这是否可行。
这是我最近的尝试:
SELECT IF (EXISTS (SELECT `Period` AS `Period` FROM myview), `PERIOD`, IF (EXISTS (SELECT `Country` AS `COUNTRY` FROM myview),`COUNTRY` FROM myview ;有任何想法吗?
编辑
我在这里看到了另一个问题: MySQL,检查SQL中是否存在列
但我仍然对if语句感到困惑。 我可以使用上面问题中的答案检查列是否存在。 但我的问题是 - 如果发现结果为真,如何从该列执行select语句。
编辑2
下面的答案表明我应该使用BEGIN和END语句,这是有道理的。 但是,我的查询在第一行抱怨。 它说'意外的IF' - 任何人都可以确认这是否是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提前致谢。
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` endThanks in advance.
最满意答案
此查询将为您提供列是否存在。
SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'db_name' AND TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name'如果要检查是否存在某些列,则执行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条件为真,那么您将执行BEGIN和END内的任何内容。
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 endIf the IF condition is true, then you will execute anything inside the BEGIN and END.
发布评论