Pull unique records from mysql where pageID 3 exists only
I have many ClientIDs to many pageids
Eg.
ClientID 1 PageID 3
ClientID 1 PageID 2
ClientID 2 PageID 3
ClientID 3 PageID 3
In a query I want to bring up the record with only PageID 3 exclusively.
开发者_开发百科So in my result i should get Client 2 and 3 and client 1 should be omitted out of the results... Whats the best way to do this?
If a client appears in more than one Page then there should be more than one row containing the clientId
SELECT * FROM tableName WHERE PageID = 3 GROUP BY ClientID HAVING ( COUNT(ClientID) = 1 )
I have not tested this but should work.
EDIT :
SELECT * FROM tableName WHERE ClientID IN (SELECT ClientID FROM tableName GROUP BY ClientID HAVING (COUNT(ClientID) = 1)) AND PageID = 3
This one is tested. The previous one does not give exclusive records.
SELECT * FROM yourTableName WHERE PageID = 3
Note by your example this gets ClientID of 1, 2, 3. However, you mentioned just wanting ClientID 2, 3.
精彩评论