mysql - 当表2值不存在时,从表1中获取记录并从另一个表中加入它

标签 mysql sql

第 1 个表 - explore_offers:

- id
- Primary Key - offer_unique

第 2 表 - participated_explore_offers:

- 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 条记录,在第二个表中有一条记录,我想要的结果是:

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

第一个表格内容:

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 条记录

最佳答案

SQL DEMO

试试这个:

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

关于mysql - 当表2值不存在时,从表1中获取记录并从另一个表中加入它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47994084/

相关文章:

mysql - select 语句引起的数据库死锁

mysql - 如何在mysql中正确使用soundex()

sql - 按列顺序获取相同的范围值

c# - 使用 INSERT 语句时如何修复 'SQLite exception, Insufficient parameters supplied to the command'

mysql - 使用 MySql 获取季度最后一个月的最后一个工作日

Mysql - 选择同一列平均值的差异

组子查询的MySQL重复键更新

sql - 在 SELECT 中创建数组

mysql - 根据先前的行填充(更新)父 ID?

mysql - 选择 2 以下列的最大值 mysql