Answer the question
In order to leave comments, you need to log in
SQL. Selecting the same values in one column, but different in another. How to implement?
Good afternoon.
There is a table like:
id | user_id | product_id
1 | 1 | 15
2 | 2 | 16
3 | 1 | 16
4 | 2 | 10
5 | 3 | 16
6 | 1 | 15
You need to select products product_id that different users have.
That is, product 16 was bought by user 1,2,3, we select it, and product 15, although it is repeated, was bought by the same user with id 1.
I tried to write something like this:
SELECT product_id FROM basket GROUP BY product_id HAVING count( *)>1;
But there are just single records. I can't make it look for the same product_id, but at the same time with different user_id.
Any help is welcome.
Answer the question
In order to leave comments, you need to log in
If MySQL can distinct in count, then
SELECT product_id
FROM basket
GROUP BY product_id
HAVING count(distinct user_id)>1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question