Redian新闻
>
How to find intersection of two tables
avatar
How to find intersection of two tables# Database - 数据库
l*n
1
Two tables A and B. A has Date and State. B also has Date and State.
A has 20 records and B has 50.
For all records, how can we find out the number of records in A that has
Date and State intersection with B.
for example, if A has
2/1/2008-3/1/2008, CA
B has
2/12-2/14/2008, TX,CA, OR
Then this record is what we want. I want to count these records.
Any way?
thanks in advance
avatar
c*d
2
呵呵,欢迎longtian版主光临数据库版
先介绍几个deal给我们吧 ;-)
估计这个就是个人使用
所以规范化,效率也就不用太严格了
tab_A (start_date, end_date, state)
tab_B (start_date, end_date, state1, state2, state3)
select * from tab_A a, tab_B b
where a.start_date < b.start_date
and a.end_date > b.end_date
and a.state in (b.state1, b.state2, b.state3)

【在 l******n 的大作中提到】
: Two tables A and B. A has Date and State. B also has Date and State.
: A has 20 records and B has 50.
: For all records, how can we find out the number of records in A that has
: Date and State intersection with B.
: for example, if A has
: 2/1/2008-3/1/2008, CA
: B has
: 2/12-2/14/2008, TX,CA, OR
: Then this record is what we want. I want to count these records.
: Any way?

avatar
B*g
3
你也搞deal?
我觉得sql应该是
select * from tab_A a, tab_B b
where a.start_date > b.end_date
and a.end_date > b.start_date
and a.state in (b.state1, b.state2, b.state3)

【在 c*****d 的大作中提到】
: 呵呵,欢迎longtian版主光临数据库版
: 先介绍几个deal给我们吧 ;-)
: 估计这个就是个人使用
: 所以规范化,效率也就不用太严格了
: tab_A (start_date, end_date, state)
: tab_B (start_date, end_date, state1, state2, state3)
: select * from tab_A a, tab_B b
: where a.start_date < b.start_date
: and a.end_date > b.end_date
: and a.state in (b.state1, b.state2, b.state3)

avatar
b*e
4
Yes. Intersection.

【在 B*****g 的大作中提到】
: 你也搞deal?
: 我觉得sql应该是
: select * from tab_A a, tab_B b
: where a.start_date > b.end_date
: and a.end_date > b.start_date
: and a.state in (b.state1, b.state2, b.state3)

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