我试图根据列和WHERE IN子句加入两个表。
SELECT DISTINCT
t1.document_num, t2.file_name
FROM
infocard_1 AS t1
INNER JOIN
web_pub_subfile AS t2 on t1.info_card_id = t2.info_card_id
WHERE
lower(t1.vault_name) LIKE N'%su-spec-release%' AND
t2.file_name =
CASE
WHEN t2.file_name IN ('00350.dwg', '00924.dwg', '00960.dwg', '00973.dwg')
THEN t2.file_name
ELSE NULL
END
我以这种格式获得输出
document_num file_name
-------------------------
SU-SH3A081 00960.DWG
SU-SH3A148 00973.DWG
但我想得到
document_num file_name
-------------------------
null 00350.dwg
null 00924.dwg
SU-SH3A081 00960.DWG
SU-SH3A148 00973.DWG
有什么方法可以实现这个目标吗?请帮我。
答案 0 :(得分:2)
您需要的是RIGHT JOIN
而不是INNER JOIN
。详细了解JOINS
here的类型。
答案 1 :(得分:1)
试试这个:
SELECT distinct t1.document_num,t2.file_name
FROM web_pub_subfile AS t2
LEFT JOIN infocard_1 AS t1 on t1.info_card_id = t2.info_card_id AND
lower(t1.vault_name) LIKE N'%su-spec-release%'
WHERE t2.file_name IN ('00350.dwg','00924.dwg','00960.dwg','00973.dwg')
答案 2 :(得分:0)
为什么CASE
中有WHERE
个表达式?这使得阅读非常困难。必要时使用AND
或OR
和括号来构建WHERE
子句。您的条款只是转换为:
WHERE lower(t1.vault_name) LIKE N'%su-spec-release%'
AND t2.file_name IN ('00350.dwg','00924.dwg','00960.dwg','00973.dwg')
要从表中获取记录,即使它们在另一个表中没有匹配项,您也要加入另一个表。
SELECT DISTINCT ic.document_num, wps.file_name
FROM web_pub_subfile wps
LEFT JOIN infocard_1 ic ON ic.info_card_id = wps.info_card_id
AND lower(ic.vault_name) LIKE N'%su-spec-release%'
WHERE wps.file_name IN ('00350.dwg','00924.dwg','00960.dwg','00973.dwg');
我用可读的东西替换了你的别名。