PHP China | 中国开源之路 's Archiver

wf7427 发表于 2008-11-19 16:54

请问这个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

keailyf 发表于 2008-11-19 17:00

用笛卡尔积去做

lxccai 发表于 2008-11-19 17:06

~~~~~

wf7427 发表于 2008-11-19 17:16

可以写出具体的语句么,谢谢

心痛 发表于 2008-11-19 21:52

[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]]

yafeikf 发表于 2008-11-19 21:54

回复 5# 心痛 的帖子

Lz要的好像是名字也相同

心痛 发表于 2008-11-19 21:56

刚才少写了 >=  2这个条件了

yafeikf 发表于 2008-11-19 21:56

回复 7# 心痛 的帖子

恩。这下Ok!

sinopf 发表于 2008-11-19 23:41

[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]

不错不错,很巧妙啊!

wf7427 发表于 2008-11-20 09:55

[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]谢谢,已解决。

cnqn 发表于 2008-11-20 10:01

count(1) from
count(1)是用来做什么的?
》=2又是什么作用?

bafndcw 发表于 2008-11-20 21:34

晕...有谁这样建表的啊..
数据冗余度太大..

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.