繁体   English   中英

我在尝试检查时遇到 sql 错误(销售 ID 不在销售详细信息中以检查是否有没有详细信息的销售)

[英]I am getting sql error while trying to check the (sales id NOT IN sales details to check if there is any sales without a details )

select id as sales_id 
from sl_sales 
where  sales_id not in (select sales_id from sl_sales_dtls  )

为什么上面的sql是错误的。 我需要检查是否有没有详细信息的销售。 谁能指导我

您不能使用在创建别名的同一范围内创建的别名。您需要这样:

select id as sales_id 
from sl_sales 
where id not in (select sales_id from sl_sales_dtls)

这是编写此查询的另一种方法,不使用内联语句。

select S.id as sales_id 
from sl_sales S
 Left join sl_sales_dtls D on S.id=D.sales_id
Where D.sales_id is null

我会使用NOT EXISTS代替:

select sl.id as sales_id 
from sl_sales sl
where not exists (select 1 from sl_sales_dtls dt where dt.sales_id = sl.id);

如果sub-query具有null值,则NOT IN将不返回任何行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM