sql-server - 如何使用连接显示不匹配的结果(sqlserver)
问题描述
我正在尝试这段代码
SELECT e.codartp AS ARTICLE,e.codartc AS COMPONENTS, sa.CODALM as WAREHOUSE,
sa.unidades as UNITS, sa.UBICACION as 'LOCATION' FROM escandallo e
left join STOCKALM sa on e.codartc = sa.codart
where trim(e.codartp) = '30393' and sa.CODALM = 0
它显示如下
ARTICLE COMPONENTS WAREHOUSE UNITS LOCATION
30393 310 0 376 XHY40081
30393 C042 0 302 NULL
30393 C042 0 1158 WCY40082
但我希望它也显示不匹配的结果。我知道那篇文章还有另一个组件,但它没有显示,因为 table stockalm 不包括库存为 0 的项目。
即使在连接表中找不到每个组件,是否也可以显示它?
非常感谢
解决方案
使用ON子句中的条件而不是Where子句
SELECT e.codartp AS ARTICLE,e.codartc AS COMPONENTS, sa.CODALM as WAREHOUSE,
sa.unidades as UNITS, sa.UBICACION as 'LOCATION' FROM escandallo e
left join STOCKALM sa on e.codartc = sa.codart AND trim(e.codartp) = '30393' and sa.CODALM = 0
另外,看看这个:
现在,此查询过滤从表 (escandallo) "trim(e.codartp) = '30393'" 和表 "STOCKALM" 中满足的所有记录为 "NULL" 显示条件不匹配
SELECT e.codartp AS ARTICLE,e.codartc AS COMPONENTS, sa.CODALM as WAREHOUSE,
sa.unidades as UNITS, sa.UBICACION as 'LOCATION' FROM escandallo e
left join STOCKALM sa on e.codartc = sa.codart and sa.CODALM = 0
where trim(e.codartp) = '30393'
推荐阅读
- react-native - 找不到变量:drawer_width
- asynchronous - 如果程序立即失败,MailboxProcessor 第一个循环将无法运行
- javascript - Javascript 只能访问内表
- javascript - 如何清除在javascript中用圆弧绘制的圆圈?
- jupyter-notebook - Jupyter ImportError:无法导入名称'create_prompt_application'ubuntu 18.04
- hadoop - 如何在 HDFS 中查找文件的创建日期
- arrays - mongodb 在聚合中使用 $count
- c++ - 在 C++ 类中分配给数组的正确方法是什么?
- java - 石头剪刀布不能按预期工作
- javascript - 如何让一个按键分别触发多个事件