当前位置:网站首页>Exists usage in SQL

Exists usage in SQL

2022-06-12 09:00:00 CSDN Q & A

There are products Product And sales records Sales Two tables

Product :

| product_id | product_name | unit_price |
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |

Sales :
| seller_id | product_id | buyer_id | sale_date | quantity | price |
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
| 2 | 1 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 3 | 2019-05-13 | 2 | 2800 |

problem

I bought S8 But I didn't buy iPhone Of buyer_id

Code

select s.buyer_id,
p.product_name
from Product p,
Sales s
where s.product_id =p.product_id
and p.product_name ='S8'
and not exists(select s.buyer_id,
p.product_name
from Product p,
Sales s
where s.product_id =p.product_id and p.product_name ='iPhone')

Wrong results and problems

The result output is empty , I'll buy it first S8 Customers , And I haven't bought iPhone, Here we use not exists Why didn't it work ?

原网站

版权声明
本文为[CSDN Q & A]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206051748568705.html