简体   繁体   English

如果所有线索均处于活动状态且引线均为0且primary_email =第二表中的是,则MySQL INNER JOIN仅从第一表中选择一行

[英]MySQL INNER JOIN select only one row from first table if all leads active = 0 and primary_email = Yes in second table

I have two tables. 我有两张桌子。 I want to join them in a way that only one record in the leads table if leads_details table all lead_id active = 0 and primary = Yes. 我想以一种方式加入他们,如果Leads_details表中的所有lead_id active = 0并且primary = Yes,则Leads表中只有一条记录。

table Lead: 表线索:

    -------------
    | id | name |
    -------------
    | 1  | abc1 |
    | 2  | abc2 |
    | 3  | abc3 |
    | 4  | abc4 |
    | 5  | abc5 |
    -------------

table LeadsDetails: 表LeadsDetails:

    --------------------------------------
    | id | lead_id | active | primary_email
    --------------------------------------
    | 1  | 1       | 1       |Yes         |
    | 2  | 1       | 0       |NO          |
    | 3  | 2       | 0       |Yes         |
    | 4  | 3       | 1       |Yes         |
    | 5  | 4       | 0       |Yes         |
    | 6  | 5       | 1       |NO          |
    -------------------------------------

expected output: 预期输出:

    --------------
    | id | name   |
    --------------
    | 1  | abc2   |
    | 2  | abc4   |
    --------------

SELECT `Lead`.`id`, `Lead`.`name`, `Lead`.`unsubscribe` 
FROM `leads` AS `Lead` inner JOIN `LeadsDetails` AS `LeadsDetails` 
ON (`LeadsDetails`.`lead_id` = `Lead`.`id`) 
WHERE `LeadsDetails`.`primary_email` = 'Yes' AND `LeadsDetails`.`active` = 0

You require group by attribute 您需要按属性分组

 SELECT `Lead`.`id`, `Lead`.`name`, `Lead`.`unsubscribe` 
FROM `leads` AS `Lead` inner JOIN `LeadsDetails` AS `LeadsDetails` 
ON (`LeadsDetails`.`lead_id` = `Lead`.`id`) 
WHERE `LeadsDetails`.`primary_email` = 'Yes' AND `LeadsDetails`.`active` = 0
GROUP BY lead.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL INNER JOIN select 第二张表只有一行 - MySQL INNER JOIN select only one row from second table 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join INNER JOIN从第二个表中仅选择一行 - INNER JOIN selects only one row from second table 内连接 select 仅从第二个表中的一行基于日期 - inner join select only one row from second table base on date 如何在不使用别名的情况下将第二个表中的一行仅插入到第一个表中 - How to INNER JOIN only 1 row from second table into the first table WITHOUT using alias MySQL连接与子查询从连接表中仅选择一行 - MySQL Join with a subquery to select only one row from joined table 内部联接MySQL表仅产生第一行 - Inner Join MySQL table only yields first row MySQL 内连接限制第二个表中的 1 行 - MySQL inner join limit 1 row from second table MySQL SELECT全部来自第一个表,并使用第二个表过滤器的特定where子句连接来自第二个表的匹配数据 - MySQL SELECT all from first table and join matched data from second table with specific where clause for second table filter 即使ID匹配,SQL INNER JOIN也仅从第二个表中选择最后一行 - SQL INNER JOIN selects only one, last row from second table even if id matches
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM