woo*_*gie 0 t-sql sql-server-2008
以下SQL语句中的内部查询是规范化部分数据库(code1,code2,code3等).使用外部查询,我想选择不在查找表中的代码(tblicd)
select primarycode from
(
select id, primarycode from myTable
union
select id, secondarycode from myTable
union
select id, tertiarycode from myTable) as t
order by id
where primarycode not in tblicd.icd_id
Run Code Online (Sandbox Code Playgroud)
上面的查询没有运行,我想知道我做错了什么.我得到的错误是the multi-part identifier tblicd.icd_id could not be bound
一个问题是你ORDER BY和WHERE条款是相反的.该ORDER BY条款必须在该WHERE条款之后.
你的WHERE条款也是不正确的.它应该是这样的:
WHERE primarycode NOT IN (SELECT icd_id FROM tblicd)
ORDER BY id
Run Code Online (Sandbox Code Playgroud)