使用'if','else'选择所需的列并进行比较

时间:2016-01-12 14:39:26

标签: mysql

我试图选择这样:

SELECT *, (SELECT IF(type=1, price_1, price_2)) as price_column WHERE price_column > 10

但是得到了这个错误:

Unknown column 'price_column' in where clause

任何提示?

3 个答案:

答案 0 :(得分:2)

这样做不简单吗?

SELECT *, IF ( id = 1, field1,field2) AS new_field
FROM table_a
having new_field > 10;

答案 1 :(得分:1)

您可以使用

SELECT * 
FROM (
     SELECT *, (SELECT IF(type=1, price_1, price_2)) as price_column
     ) AS Tab1
WHERE price_column > 10

答案 2 :(得分:1)

首先你从表中遗漏了,你可以使用它:

SELECT *, (SELECT IF(type=1, price_1, price_2)) as price_column 
from table_name WHERE (SELECT IF(type=1, price_1, price_2)) > 10