我有以下声明
SELECT disease.id, disease.name, disease_synonym.name FROM disease JOIN disease_synonym where diseaseId=code
结果是一个带有id和两列名称的表。如何将其转换为只有id和名称的2列? (当然,id现在会出现几次)
再见,
尼科
答案 0 :(得分:4)
有两种想法......
运行查询两次(一次用于名称,一次用于同义词),然后将结果合并在一起......
SELECT disease.id, disease.name FROM disease
UNION ALL
SELECT disease.id, disease_synonym.name FROM disease JOIN disease_synonym where diseaseId=code
或者加入两行表,并使用CASE语句来执行数据透视...
SELECT
disease.id,
CASE WHEN pivot.field = 'name' THEN disease.name
WHEN pivot.field = 'syno' THEN disease_synonym.name
END
FROM
disease
INNER JOIN
disease_synonym
ON diseaseId=code
CROSS JOIN
(SELECT 'name' AS field UNION ALL SELECT 'syno' AS field) AS Pivot
答案 1 :(得分:0)
我想你想SELECT disease.id, disease_synonym.name FROM ...
不是吗?只显示同义词名称?