For a table (Ingredients) like this:
+-----+-----+
|id |fruit|
+-----+-----+
| 1 | or |
| 1 | ban |
| 2 | or |
| 2 | or |
+-----+-----+
I want to have a query that should produce a result like this:
id 1 is associated with two types of fruits.
I do not care if multiple rows have the same data, I only care if rows with the same id have different fruits.
e.g. id 2 is only associated with one type of fruit in both rows, i do not care about that!!!
I have pseudo code for what I want to achieve but not any mysql
Select all id's
From the ingredients table
Group by id
If group count of id is greater than 1 and has different fruits then display
I have a funny feeling I might not be able to do this in Mysql and will have to use some PHP.
Thanks for your time
Your pseudo code in SQL:
and if it’s slow, you can try this version –
MAX()andMIN()may be faster thanCOUNT(DISTINCT )– although it’s less readable: