Nadia
Nadia

Reputation: 41

SQL is returning distinct values without the distinct word

My sql query is returning distinct values. This is the query:

select * 
from Products 
where [Product_ID] in (select Product_Id f 
                       from MyCart 
                       where User_Id = '5570928b-7a1b-4652-9c6b-592e76a70a07')

The second select query is returning (7,7,3) and the first select is returning information only for one 7 and 3.

I suppose it is because the 7's are duplicates, but I need the result to contain information about all the products in the second select, no matter if they are duplicate or not.

Upvotes: 1

Views: 77

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270331

In that case, use JOIN:

select p.*
from Products p join
     MyCart c
     on p.Product_Id = c.Product_Id
where c.User_Id = '5570928b-7a1b-4652-9c6b-592e76a70a07';

Usually, duplicates are undesirable, which is why EXISTS and IN are used.

Upvotes: 6

Related Questions