首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

一个很扭结的查询语句

2012-09-21 
一个很纠结的查询语句一个表name store两列有n条数据要得到name列中名字相同的行中store列值唯一的比如nam

一个很纠结的查询语句
一个表 
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)

看看哪个快。

热点排行