0

I am trying to write a query that will return rows that have two matching fields.

|-------------|-------------|----------|
|      id     |   field 1   |  field 2 |
|-------------|-------------|----------|
|      1.     |      a      |     z    | #
|-------------|-------------|----------|
|      2.     |      b      |     x    |
|-------------|-------------|----------|
|      3.     |      c      |     y    | ##
|-------------|-------------|----------|
|      4.     |      a      |     z    | #
|-------------|-------------|----------|
|      5.     |      b      |     z    |
|-------------|-------------|----------|
|      6.     |      c      |     y    | ##
|-------------|-------------|----------|

The rows marked above would be returned from such a query. They are essentially duplicates. I tried inner joining the table on itself where field 1 = field 1 and field 2 = field 2, to no avail. Can this be done without writing a PHP script with multiple queries?

DrDwan
  • 1
  • 2

2 Answers2

0

Sorry, I misunderstood. You are looking for something like this:

SELECT GROUP_CONCAT(t1.id), t1.f1, t1.f2 
FROM t t1 
CROSS JOIN t t2 
WHERE t1.f1=t2.f1 AND t2.f2=t1.f2 AND t1.id != t2.id
GROUP BY t1.f1, t1.f2
Frank Conry
  • 2,694
  • 3
  • 29
  • 35
0
SELECT t.*
FROM mytable t
INNER JOIN (
   SELECT field_1, field_2
   FROM mytable
   GROUP BY field_1, field_2
   HAVING COUNT(id)>1
) f
ON t.field_1 = f.field_1
   AND t.field_2 = f.field_2
Alex
  • 16,739
  • 1
  • 28
  • 51