我有以下查询:
SELECT X.ID, X.NAME, CASE R.ID WHEN NOT NULL THEN 1 ELSE 0 END ISRELATED
FROM TABLE X
INNER JOIN RELATIVE R ON R.NAMEID = X.ID
我试图让结果看起来像这个结构,而不是从表X中拉出任何重复的行:
ID
NAME
ISRELATED (boolean)
有什么建议吗?感谢。
答案 0 :(得分:2)
使用INNER JOIN将仅返回X中具有R中相应行的那些行。使用Left outer join代替。
SELECT DISTINCT X.ID, X.NAME, CASE WHEN R.ID IS NOT NULL THEN 1 ELSE 0 END ISRELATED
FROM TABLE X
LEFT OUTER JOIN RELATIVE R ON R.NAMEID = X.ID
答案 1 :(得分:0)
试试这个
SELECT DISTINCT X.ID, X.NAME, CASE WHEN R.ID IS NOT NULL THEN 1 ELSE 0 END ISRELATED
FROM TABLE X
INNER JOIN RELATIVE R ON R.NAMEID = X.ID
答案 2 :(得分:0)
我认为您在CASE
中有错误,必须使用LEFT JOIN
SELECT DISTINCT
X.ID,
X.NAME,
CASE WHEN R.ID IS NOT NULL THEN 1 ELSE 0 END ISRELATED
FROM TABLE X
LEFT JOIN RELATIVE R ON R.NAMEID = X.ID