admin

当表2值不存在时,从表1中获取记录,并从另一个表中将其联接

sql

第一张表-Explore_offers:

- id
- Primary Key - offer_unique

第2个表-参与调查的报价:

- id
- email - user_email
- Primary Key - offer_unique

我想要的是:*显示第一个表记录,并排除那些记录,该记录位于第二个表中并找到特定的电子邮件

前任:

SELECT eo.*
     , peo.user_email 
  FROM explore_offers eo 
  LEFT 
  JOIN participated_explore_offers peo 
    ON eo.offer_unique = peo.offer_unique
 WHERE peo.user_email = 'test@gmail.com'

我已经尝试过该示例,并且得到0条记录。我在第一个表中有2条记录,在第二个表中有2条记录,我想要的结果是:

*。从第一个表中获得一条记录,而该记录在第二个表中不存在。

第一表内容:

Nr id  Primary Key
1  0   m1
2  1   m2

第二表内容

Nr id user_email      Primary Key
1  0  test@gmail.com  m1
1  0  test2@gmail.com  m2

预期的

Nr id Primary Key
1  1  m2

我所拥有的:

0条记录


阅读 196

收藏
2021-07-01

共1个答案

admin

SQL演示

试试这个 :

select * from explore_offers
where offer_unique not in 
(select offer_unique from participated_explore_offers where user_email='test@gmail.com')
2021-07-01