2017-12-27 99 views
1

1表 - explore_offers:获取从表1中的记录,并从另一个表连接它时,表2的值不存在

- id 
- Primary Key - offer_unique 

第2表 - participated_explore_offers:

- id 
- email - user_email 
- Primary Key - offer_unique 

我想要什么: *显示第一张表的记录,并排除第二张表中发现的特定电子邮件的记录

ex:

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 = '[email protected]' 

我已经试过了例子,我得到0的记录。 我有2个记录的第一个表,一个在第二个表,我想结果是:

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

1表内容:

Nr id Primary Key 
1 0 m1 
2 1 m2 

第二表内容

Nr id user_email  Primary Key 
1 0 [email protected] m1 
1 0 [email protected] m2 

预计

Nr id Primary Key 
1 1 m2 

我有什么:

0条记录

+1

您试图解释,但提供一些样本数据和来自提供数据的预期输出要好得多。 –

+0

添加了内容和预期。 – Jaeger

回答

2

SQL DEMO

试试这个:

select * from explore_offers 
where offer_unique not in 
(select offer_unique from participated_explore_offers where user_email='[email protected]') 
+0

这是正确的,但对我的问题不正确。此查询只显示没有任何电子邮件的记录,但是我将拥有大量带有不同电子邮件的记录,并且我希望查询针对所选电子邮件。 – Jaeger

+0

检查更新的答案 –

+0

编辑,其正确的!谢谢。 – Jaeger

1

移动电子邮件filteration到JOIN条件,使其与LEFT JOIN工作:

SELECT eo.*,peo.user_email 
FROM explore_offers eo 
LEFT JOIN participated_explore_offers peo ON (eo.offer_unique = peo.offer_unique) 
              AND peo.user_email = '[email protected]' 
WHERE peo.user_email is null; 

demo

| Nr | id | offer_unique | user_email | 
|----|----|--------------|------------| 
| 2 | 1 |   m2 |  (null) | 
+0

此查询显示有电子邮件的记录,我想排除它。 – Jaeger

+0

@Jaeger所以你想不具备电子邮件'测试@ gmail.com'这些记录?或者是什么?? – 2017-12-27 14:55:18

+0

是的,我想从1号表,它不包含从2表该电子邮件的记录。 – Jaeger

相关问题