请问这个mysql语句应当如何写
我有个表test,有两个字段prize_id和name,具体如下prize_id name
6 wang
7 wang
6 zhang
8 zhang
7 lee
6 lee
7 zhang
6 liu
在这个表,我想找相同name的,并且prize_id是6和7的,而且prize_id不等于8的,请问这个mysql语句应当如何写那?
也就是说我想要的结果为:
prize_id name
6 wang
7 wang
7 lee
6 lee 用笛卡尔积去做 ~~~~~ 可以写出具体的语句么,谢谢 [php]
select * from table a where (select count(1) from table b where b.name = a.name and (b.prize_id = 6 or b.prize_id = 7) ) >= 2
[/php]
试试这个,最近碰到好多用类似语句解决的SQL
[[i] 本帖最后由 心痛 于 2008-11-19 21:55 编辑 [/i]]
回复 5# 心痛 的帖子
Lz要的好像是名字也相同 刚才少写了 >= 2这个条件了回复 7# 心痛 的帖子
恩。这下Ok! [quote]原帖由 [i]心痛[/i] 于 2008-11-19 21:52 发表 [url=http://bbs.phpchina.com/redirect.php?goto=findpost&pid=733789&ptid=91990][img]http://bbs.phpchina.com/images/common/back.gif[/img][/url]select * from table a where (select count(1) from table b where b.name = a.name and (b.prize_id = 6 or b.prize_id = 7) ) >= 2
试试这个,最近碰到好多用类似语句解决的SQL [/quote]
不错不错,很巧妙啊! [quote]原帖由 [i]心痛[/i] 于 2008-11-19 21:52 发表 [url=http://www.phpchina.com/bbs/redirect.php?goto=findpost&pid=733789&ptid=91990][img]http://www.phpchina.com/bbs/images/common/back.gif[/img][/url]
select * from table a where (select count(1) from table b where b.name = a.name and (b.prize_id = 6 or b.prize_id = 7) ) >= 2
试试这个,最近碰到好多用类似语句解决的SQL [/quote]谢谢,已解决。 count(1) from
count(1)是用来做什么的?
》=2又是什么作用? 晕...有谁这样建表的啊..
数据冗余度太大..
页:
[1]
