July 2, 2015 at 6:37 am
Hi all,
Bit of a noob, still lots to learn lol
Trying to do a many to many query as follows:
SELECT * FROM clients
INNER JOIN clientscategories
ON clientscategories.idclient = clients.idclient
WHERE clientscategories.idcat IN (12, 16);
............
Values 12 & 16 are just random values for test purposes. Anyway the problem I have is if a client is in more than one category e.g. 12 & 16 then the client is retrieved multiple times. How to I need to change my query to only get a client once no matter how many cats they are in.
Thanks
July 2, 2015 at 6:43 am
Without sample data or a table to test against it's a guess, but you probably want to use an EXISTS or an IN, not a join.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 2, 2015 at 6:46 am
As I said a NOOOOOB
My table layout is
Not sure how I would go about without using a JOIN ????
Thank you for your help.
July 2, 2015 at 6:49 am
mattmacmillan (7/2/2015)
Hi all,Bit of a noob, still lots to learn lol
Trying to do a many to many query as follows:
SELECT * FROM clients
INNER JOIN clientscategories
ON clientscategories.idclient = clients.idclient
WHERE clientscategories.idcat IN (12, 16);
............
Values 12 & 16 are just random values for test purposes. Anyway the problem I have is if a client is in more than one category e.g. 12 & 16 then the client is retrieved multiple times. How to I need to change my query to only get a client once no matter how many cats they are in.
Thanks
Welcome to the forums.
The way your query is written it will return everything in both tables. There may well be a couple of ways of solving this but it'll be much easier to give you precise advice if you post some sample data. Have a read of the link if my signature to see how.
At face value, you could try selecting only the columns you actually want to see and using SELECT DISTINCT if the rows are unique.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
July 2, 2015 at 6:51 am
mattmacmillan (7/2/2015)
My table layout is
Which doesn't help me to test queries that I write 😉
Something like
SELECT <stuff>
FROM Clients c
WHERE c.idclient IN (SELECT cc.idclient FROM clientscategories cc WHERE cc.idcat IN (12, 16));
If you want tested and working queries in the future, what we ask here is for the table definitions in a format that allows me to easily create them on my machine, ie CREATE TABLE statements and sample data that's easy to use ie INSERT statements. Especially important when the problem is complex, as showing what's wanted is often easier than explaining it.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 2, 2015 at 6:52 am
Wow some seriously fast responses here. Thanks
I will try what you have posted and if I get nowhere will post some sample data up. Thanks a lot.
July 2, 2015 at 6:56 am
BWFC (7/2/2015)
At face value, you could try selecting only the columns you actually want to see and using SELECT DISTINCT if the rows are unique.
Would not be my first, second or third choice. Probably my last resort. Selecting too much data and then using DISTINCT to 'fix' it is generally a poor practice.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 2, 2015 at 7:03 am
GilaMonster (7/2/2015)
BWFC (7/2/2015)
At face value, you could try selecting only the columns you actually want to see and using SELECT DISTINCT if the rows are unique.Would not be my first, second or third choice. Probably my last resort. Selecting too much data and then using DISTINCT to 'fix' it is generally a poor practice.
True, but it was one option, presented as something the OP could try, with nothing much to go on. It is something I always try and avoid though.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
July 2, 2015 at 7:10 am
Thank you , this did solve it.
Appreciate all responses. Cheers
GilaMonster (7/2/2015)
mattmacmillan (7/2/2015)
My table layout isWhich doesn't help me to test queries that I write 😉
Something like
SELECT <stuff>
FROM Clients c
WHERE c.idclient IN (SELECT cc.idclient FROM clientscategories cc WHERE cc.idcat IN (12, 16));
If you want tested and working queries in the future, what we ask here is for the table definitions in a format that allows me to easily create them on my machine, ie CREATE TABLE statements and sample data that's easy to use ie INSERT statements. Especially important when the problem is complex, as showing what's wanted is often easier than explaining it.
July 2, 2015 at 7:20 am
Do you understand why and how it works?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply