如果表具有相同的列名,如何仅在左连接中打印列名称

时间:2015-08-17 03:55:11

标签: php mysql

 SELECT A.status, B.status, C.status
 FROM A
 LEFT JOIN B ON A.id = B.a_id
 LEFT JOIN C ON A.id = c.a_id

我的查询输出:

status status status
--------------------
1       1     NULL
0      NULL    1
1       0      NULL
0      NULL     0

我不想要空值。

我想要这样的输出

 Id(A table primary key ) status
 --------------------------------
 1                           1
 2                           0
 3                           1    
 4                           0

3 个答案:

答案 0 :(得分:0)

SELECT A.status as status FROM A left JOIN B ON A.id = B.a_id left JOIN c ON A.id = c.a_id

答案 1 :(得分:0)

试试这种方式

SELECT A.status 'A-Status', B.status 'B-Status',c.status 'C-Status'
 FROM A
 left JOIN B ON A.id = B.a_id
 left JOIN c ON A.id = c.a_id

答案 2 :(得分:0)

SELECT A.status, 
    (CASE 
        WHEN ( A.status =1 ) THEN 
            B.status ELSE c.status 
        END 
    ) 
    AS ss 
    FROM A 
    LEFT JOIN B ON A.id = B.a_id 
    LEFT JOIN c ON A.id = c.a_id