Exclusive records MySQL
I have many ClientIDs to many pageids
Eg.
ClientID 1 PageID 3
ClientID 1 PageID 1 ClientID 2 PageID 3 ClientID 2 PageID 2开发者_Go百科 ClientID 3 PageID 3In a query I want to bring up the record with only PageID 3 and PageID 2 exclusively.
So in my result I should get Client 2 and 3 and client 1 should be omitted out of the results... What's the best way to do this?
Answer:
SELECT clientID, clientName, pageID, profileTypeID, pageName
FROM client
GROUP BY
clientID
HAVING
COUNT(*) = COUNT(IF(profileTypeID != 1, profileTypeID, NULL))
The way I'm reading this is that you want any row where the given ClientID is not associated with a Page other than 2 or 3.
Select ...
From MyTable As T
Where T.PageID In(2,3)
And Not Exists (
Select 1
From MyTable As T2
Where T2.ClientID = T.ClientId
And T2.PageID Not In(2,3)
)
精彩评论