日期:2014-05-16  浏览次数:20802 次

一个很纠结的查询语句
一个表 
name store两列 有n条数据

 要得到name列中名字相同的行中store列值唯一的
比如
name store
a 11
a 22
a 11
a 33
b 44
b 55
b 44
c 66
d 77
d 77

要得到  
a 22
a 33
b 55
c 66
行  
请教各位大神,这个sql语句怎么写 尽量短点的

------解决方案--------------------
mysql> select name,store from tt group by store having count(*)=1;
+------+-------+
| name | store |
+------+-------+
| a | 22 | 
| a | 33 | 
| b | 55 | 
| c | 66 | 
+------+-------+
4 rows in set (0.02 sec)
------解决方案--------------------
mysql> select name,store from (select name,store,count(*) total from tt group by store) t where total=1;
+------+-------+
| name | store |
+------+-------+
| a | 22 | 
| a | 33 | 
| b | 55 | 
| c | 66 | 
+------+-------+
4 rows in set (0.02 sec)

看看哪个快。