如何组合两个表中的列和条件语句创建的别名?

时间:2017-12-26 21:52:52

标签: mysql

这种方法不起作用,因为它会产生语法错误。

SELECT A.Field1, A.Field2, B.Field3, B.Field4, Field5,
IF(A.SomeField = "","No","Yes") AS Field5;
FROM Atable A, Btable B
WHERE A.Field1 = B.FieldX;

1 个答案:

答案 0 :(得分:2)

使用CASE WHEN

SELECT A.Field1, A.Field2, B.Field3, B.Field4, 
    CASE WHEN A.Field1 = "" 
    THEN 'NO' ELSE 'YES' END AS Field5
FROM Atable A
JOIN Btable B on B.FieldX = A.Field1