I have a table of data, and I want to be able to output all the data that does not repeat the contents of two columns, an illustration:
Example table:
Column1 | Column2 | Column3
1 XX pop
2 YY yif
3 ZZ pop
4 PP pop
5 XX pop
6 YY yif
7 PP Hor
8 MM tre
9 PP pop
10 XX pop
11 MM pop
What I want to do here is output all rows where the values for both Columns 2 and 3 are not repeated, so you will see that Column1
== 1 and Column1
== 5 have the same values in both Column2
and Column3
, so they only need to output where Column1
== 1 and not =5 (or =10).
I would like to set the query to output all rows, except where Column1
== 5,6,9,10 because these have both Column2
and Column3
repetition.
Intended output of query: (note: all three columns are required)
Column1 | Column2 | Column3
1 XX pop
2 YY yif
3 ZZ pop
4 PP pop
7 PP Hor
8 MM tre
11 MM pop
I have tried looking into Group By but this only appears to group by one column, or at least groups them from left to right so GROUP BY (Column2, Column3)
would GROUP BY
all values in Column2
and then values in Column3
rather than treat both columns equally.
I found one possible solution which was to concat the columns beforehand, such as
GROUP BY CONCAT(Column2, '_', Column3)
From Is it possible to GROUP BY multiple columns using MySQL? but this has been vaguely criticised (at least, as an answer to that question), but seems the closest code I've seen to do what I want to do.
So, how should I structure my
GROUP BY
clause to reach this outcome?Is the
GROUP BY CONCAT
I have found a good way of approaching this?