Redian新闻
>
我也问一个sql querry的问题
avatar
我也问一个sql querry的问题# Database - 数据库
s*f
1
比如table是这样的
id-----year-----month------score
1-----2000-------1----------100
2-----2000-------1----------100
2-----2000-------2----------300
2-----2000-------3----------300
3-----2000-------3----------200
....
如果我想找出2000年2月和3月都有记录而且相等的id,请问这个query怎么写法? 谢谢
avatar
c*t
2
self join...

【在 s********f 的大作中提到】
: 比如table是这样的
: id-----year-----month------score
: 1-----2000-------1----------100
: 2-----2000-------1----------100
: 2-----2000-------2----------300
: 2-----2000-------3----------300
: 3-----2000-------3----------200
: ....
: 如果我想找出2000年2月和3月都有记录而且相等的id,请问这个query怎么写法? 谢谢

avatar
c*e
3
select a.id
from (select id, score from your_table
where year = 2000 and month = 2) a
join (select id, score from your_table
where year = 2000 and month = 3) b
on a.id = b.id
and a.score = b.score
group by 1 ;

【在 s********f 的大作中提到】
: 比如table是这样的
: id-----year-----month------score
: 1-----2000-------1----------100
: 2-----2000-------1----------100
: 2-----2000-------2----------300
: 2-----2000-------3----------300
: 3-----2000-------3----------200
: ....
: 如果我想找出2000年2月和3月都有记录而且相等的id,请问这个query怎么写法? 谢谢

avatar
m*g
4
用小钻风和总钻风比就可以了

【在 s********f 的大作中提到】
: 比如table是这样的
: id-----year-----month------score
: 1-----2000-------1----------100
: 2-----2000-------1----------100
: 2-----2000-------2----------300
: 2-----2000-------3----------300
: 3-----2000-------3----------200
: ....
: 如果我想找出2000年2月和3月都有记录而且相等的id,请问这个query怎么写法? 谢谢

avatar
B*g
5
self join 比 nest table 好吗?

谢谢

【在 c*******e 的大作中提到】
: select a.id
: from (select id, score from your_table
: where year = 2000 and month = 2) a
: join (select id, score from your_table
: where year = 2000 and month = 3) b
: on a.id = b.id
: and a.score = b.score
: group by 1 ;

avatar
c*e
6
看table吧
我那个query可能看上去比较清楚

【在 B*****g 的大作中提到】
: self join 比 nest table 好吗?
:
: 谢谢

avatar
B*g
7
xixi.
That is what i think everyone ask questions here should pay attention to.
Give more information about the tables(data, index....), otherwise, it is
very hard to find out the best solution.
Like this sample, if ID+Score is unique for each month+year (That is what it
should be, I guess), my solution is
SELECT id
FROM your_table
WHERE year = 2000
AND (month = 2 OR month = 3)
GROUP BY id, score
HAVING COUNT(*) > 1

【在 c*******e 的大作中提到】
: 看table吧
: 我那个query可能看上去比较清楚

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。