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.
table Lead:
-------------
| id | name |
-------------
| 1 | abc1 |
| 2 | abc2 |
| 3 | abc3 |
| 4 | abc4 |
| 5 | abc5 |
-------------
table 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.