我有两个选择语句:
SELECT '1' AS ID , 'JOHN' AS SURNAME, NULL AS FIRSTNAME
SELECT '1' AS ID, NULL AS SURNAME, 'BLOGGS' AS FIRSTNAME
如何以两行合二为一的方式在结果中得到这个?
1 | JOHN | BLOGGS
答案 0 :(得分:1)
您可以使用union all
和聚合:
SELECT ID, MAX(SURNAME) as SURNAME, MAX(FIRSTNAME) as FIRSTNAME
FROM (SELECT '1' AS ID , 'JOHN' AS SURNAME, NULL AS FIRSTNAME
UNION ALL
SELECT '1' AS ID, NULL AS SURNAME, 'BLOGGS' AS FIRSTNAME
) t
GROUP BY ID;