SQL where 子句在 sub-table 中查找包含混合值的记录
SQL where clause to find records containing mixed values in a sub-table
我是 SQL 的超级新手。
我正在编写一个相当简单的查询,但遇到了问题。
我工作的公司在不同站点之间转移货物,我们通过两个主要 table 跟踪这些货物。 Table1是转运header,Table2是正在转运的货物。当货物在接收端被预订时,这会更新 Table2 中的状态字段,当整个传输被预订时,这会更新 header table.
中的状态
我正在尝试查找尚未预订所有产品但错误地更新了 header 的转账记录 header。
我努力使 WHERE 子句正确,我尝试了以下及其变体,但没有成功。
select distinct Table1.ref_no, Table1.status, Table1.site_to
from Table1 with(nolock)
left join Table2 with(nolock)
on Table2.ref_no = Table1.ref_no
where (Table2.status ='notbooked' and Table2.status ='booked')
SELECT DISTINCT Table2.ref_no FROM Table2 INNER JOIN Table1 ON Table2.ref_no = Table1.ref_no
WHERE Table2.status = 'notbooked' AND Table1.status = 'booked'
你至少需要看看你的 where 语句。
Table2.status 永远不可能是 'notbooked' 和 'booked'
我想你的意思是:
table2.status = 'notbooked' and table1.status = 'booked'
我是 SQL 的超级新手。
我正在编写一个相当简单的查询,但遇到了问题。
我工作的公司在不同站点之间转移货物,我们通过两个主要 table 跟踪这些货物。 Table1是转运header,Table2是正在转运的货物。当货物在接收端被预订时,这会更新 Table2 中的状态字段,当整个传输被预订时,这会更新 header table.
中的状态我正在尝试查找尚未预订所有产品但错误地更新了 header 的转账记录 header。
我努力使 WHERE 子句正确,我尝试了以下及其变体,但没有成功。
select distinct Table1.ref_no, Table1.status, Table1.site_to
from Table1 with(nolock)
left join Table2 with(nolock)
on Table2.ref_no = Table1.ref_no
where (Table2.status ='notbooked' and Table2.status ='booked')
SELECT DISTINCT Table2.ref_no FROM Table2 INNER JOIN Table1 ON Table2.ref_no = Table1.ref_no
WHERE Table2.status = 'notbooked' AND Table1.status = 'booked'
你至少需要看看你的 where 语句。
Table2.status 永远不可能是 'notbooked' 和 'booked'
我想你的意思是:
table2.status = 'notbooked' and table1.status = 'booked'