maybe someone can help me. We have two tables:
A company_project may have more than one event with different types (event_type_id). If a Project has a special event I want to remove it from the results even if it has other events.
This doesnt work:
SELECT cp.id, ce.event_type_id
FROM companies_projects cp
LEFT JOIN companies_events ce ON ce.project_id = cp.id
WHERE ce.event_type_id != 219
GROUP BY cp.id
Table structure like this:
**company_projects**
id
project_type_id
company_id
...
**company_events**
id
project_id
event_type_id
...
I just want to leave out the projects that have a special event, even if it has other events.
You need to exclude the project record, not the event, so:
SELECT cp.id, ce.event_type_id
FROM companies_projects cp
LEFT JOIN companies_events ce ON ce.project_id = cp.id
WHERE cp.id NOT IN (
SELECT cp.id FROM companies_projects cp
LEFT JOIN companies_events ce ON ce.project_id = cp.id
WHERE ce.event_type_id = 219
)
GROUP BY cp.id
SELECT cp.id, ce.event_type_id
FROM companies_projects cp
LEFT JOIN companies_events ce
ON ce.project_id = cp.id
WHERE NOT EXISTS(SELECT NULL
FROM companies_events
WHERE project_id = cp.id
AND event_type_id = 219)
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.