求一句SQL(去除表中只出现过一次行)
表内容如:
id name
1 AAA
2 AAA
3 BBB
4 CCC
5 BBB
希望取出
id name
1 AAA
2 AAA
3 BBB
5 BBB
就是希望不要取出仅仅现过一次的CCC这一行。
谢谢!
------解决方案--------------------select * from a join (select name from a group by name having count(*) > 1) as b on a.name = b.name
------解决方案--------------------mysql> create table t1
-> (
-> id int not null auto_increment primary key,
-> name char(3)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> insert into t1 (name) values
-> ( 'AAA '),( 'AAA '),( 'BBB '),( 'CCC '),( 'BBB ');
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | AAA |
| 2 | AAA |
| 3 | BBB |
| 4 | CCC |
| 5 | BBB |
+----+------+
5 rows in set (0.02 sec)
mysql> select * from t1 group by name having count(name) > 1;
+----+------+
| id | name |
+----+------+
| 1 | AAA |
| 3 | BBB |
+----+------+
2 rows in set (0.00 sec)
------解决方案--------------------select * from a where id in (select id from a group by name having count(*) > 1)
------解决方案--------------------select * from tt where t not in (select t from tt group by t2 having count(*) = 1)