简体   繁体   中英

SQL query with filters

I'm trying to run a SQL query but I'm not sure how to accomplish. So I have the following query that outputs the below results:

SELECT
    events.Name, results.Description, Events.Id, 
    results.EventId, results.UserId
FROM
    Events
LEFT JOIN 
    Results ON events.Id = Results.EventId

查询结果:

What I'm trying to do is filter out UserId = 66 and the associated eventId . So the results table should exclude rows with UserId = 66 and rows with EventId = 4 .

So the results table should exclude rows with UserId 66 and rows with EventId 4.

Well, you may want:

SELECT e.Name, r.Description, e.Id, r.EventId, r.UserId
FROM Events e JOIN
     Results r
     ON e.Id = r.EventId
WHERE r.UserId <> 66 AND e.eventId <> 4;

This would appear the answer the question that I've quoted. However, your query is using a LEFT JOIN and other parts of your question are somewhat ambiguous.

For instance, if you want these removed fro your specific query:

SELECT e.Name, r.Description, e.Id, r.EventId, r.UserId
FROM Events e LEFT JOIN
     Results r
     ON e.Id = r.EventId AND e.eventId <> 4
WHERE r.UserId <> 66 ;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM