在sql中使用联接列出数据

qvk1mo1f  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(246)

关于在sql中将表连接在一起,我有以下问题:
全部列出 products 尚未售出的 13.05.2003 . 考虑到表格 Orders , LineItem 以及 Product .

我的代码如下:

SELECT p.Pid, p.Label
 from Product p natural join line_item l
natural join Orders
where Date <> "2003-05-13"

问题是,当我执行这段代码时,它看起来应该是更多的数据,而且我不知道如何使用 join .
先谢谢你

ldioqlga

ldioqlga1#

使用起来更安全 NOT EXISTS . 例如:

select *
from product p
where not exists (
  select null 
  from lineitem i
  join orders o on o.oid = l.oid and l.pid = p.pid
  where date <> "2003-05-13"
)

或者你可以用 NOT IN :

select *
from product 
where pid not in (
  select l.pid
  from lineitem i
  join orders o on o.oid = l.oid
  where date <> "2003-05-13"
)
ylamdve6

ylamdve62#

最后我尝试了以下方法,效果很好:

select product.pid, product.label
from ((product  inner join line_item on product.pid = line_item.pid) inner join orders on orders.oid = line_item.oid)
where not orders.date ='2003-05-13'
group by product.pid;

非常感谢你的回答,他们帮我找到了解决问题的正确途径

相关问题