简体   繁体   中英

MySQL query for multiple rows

I have a mySql table ... quite simple (id, firstColumn, secondColumn) I want to make a query who display me the duplicate values in secondColumn with the same value in firstColumn

If i have something like that

1, 14, 1
2, 14, 2
3, 15, 1
4, 15, 2
5, 14, 2
6, 15, 1
7, 16, 1
8, 17, 1

my query to display duplicate values

5, 14, 2
6, 15, 1

Thanks

Try this:

SELECT column1, column2, COUNT(*)
FROM tableNAME
GROUP BY column1, column2
HAVING COUNT(*) > 1

Solution 1:

SELECT DISTINCT
  t1.id,
  t1.firstColumn
FROM
  tablename t1
INNER JOIN
  tablename t2
ON
  t1.firstColumn = t2.firstColumn

Solution 2:

SELECT
  id,
  firstColumn
FROM
  tablename
GROUP BY
  id, firstcolumn
HAVING
  COUNT(*) > 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM