如何获得不明显的值并显示数据

时间:2014-06-13 02:49:42

标签: mysql sql

示例表

从表中选择LastName,FirstName;


| LastName   |   Firstname|
|            |            |
| Dela Cruz  |    Juan    |
| Dela Cruz  |    John    |
| Simon      |    Lucy    |
| Stin       |    Joy     |

如何查询此输出,或查看具有非独特值的数据:

| LastName   |   Firstname|
|            |            |
| Dela Cruz  |    Juan    |
| Dela Cruz  |    John    |

1 个答案:

答案 0 :(得分:0)

SELECT m.`LastName` AS LName
     , m.`FirstName` AS FName
     , m.`MiddleName` AS MName
FROM table m JOIN ( SELECT `LastName`
                    FROM `table` 
                    GROUP BY `LastName`
                    HAVING COUNT(*) > 1 ) x ON x.`LastName` = m.`LastName`

SQLFiddle