Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Join and exclude / filter

Tags:

sql

mysql

I've got an SQL query which joins 2 tables, I'm trying to filter rows which match a condition, then filter out the results if the same condition matches with different values, but the last WHERE clause seems to be ignored:

SELECT DISTINCT
    tblclients.firstname, tblclients.lastname, tblclients.email
FROM
    tblclients
LEFT JOIN
    (
        SELECT *
        FROM tblhosting
        WHERE tblhosting.packageid IN (75,86)
    ) tblhosting ON tblclients.id = tblhosting.userid
WHERE 
    tblhosting.packageid NOT IN (76,77,78)

The idea being to get a list of customers which have a certain package (ID 75 and 86), then exclude/take out any results/customers which also have another package as well as 75/86 (ID 76,77,78 etc). It's not excluding those results though at all, tried numerous variations here on Stackoverflow, where am I going wrong please?

like image 308
Nick Avatar asked Sep 18 '26 17:09

Nick


1 Answers

Add it to the join condition itself. When you have a where clause filter your join would be treated as inner join.

tblhosting ON tblclients.id = tblhosting.userid and tblhosting.packageid NOT IN (76,77,78)
like image 175
Vamsi Prabhala Avatar answered Sep 21 '26 08:09

Vamsi Prabhala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!