avatar
SQL Server query 一问# Database - 数据库
a*8
1
现有3张表A, B, C
A (Id1, Id2) with data
1 1
1 2
1 3
1 4
avatar
n*6
2
我的土办法,不知是否可行。
1。把id1, id2连起来,变成id3。
2。
SELECT A.ID3, B.ColumnB1, B.ColumnB2, C.ColumnC1, C.ColumnC2
FROM A LEFT JOIN B ON A.ID3 = B.ID3
RIGHT JOIN C ON A.ID3 = B.ID3
细节和优化方案正在思索中。
avatar
B*g
3
SELECT a.id1, a.id2, b.ColumnB1, b.ColumnB2, NULL ColumnC1, NULL ColumnC2
FROM A a, B b
WHERE a.id1 = b.id2
AND a.id2 = b.id2
UNION ALL
SELECT a.id1, a.id2, NULL ColumnB1, NULL ColumnB2, ColumnC1, ColumnC2
FROM A a, C c
WHERE a.id1 = c.id2
AND a.id2 = c.id2
有包子吗?

【在 a******8 的大作中提到】
: 现有3张表A, B, C
: A (Id1, Id2) with data
: 1 1
: 1 2
: 1 3
: 1 4

avatar
a*8
4
结果似乎不对,不过还是给个包子。

【在 n********6 的大作中提到】
: 我的土办法,不知是否可行。
: 1。把id1, id2连起来,变成id3。
: 2。
: SELECT A.ID3, B.ColumnB1, B.ColumnB2, C.ColumnC1, C.ColumnC2
: FROM A LEFT JOIN B ON A.ID3 = B.ID3
: RIGHT JOIN C ON A.ID3 = B.ID3
: 细节和优化方案正在思索中。

avatar
a*8
5
十分感谢,包子送上!

【在 B*****g 的大作中提到】
: SELECT a.id1, a.id2, b.ColumnB1, b.ColumnB2, NULL ColumnC1, NULL ColumnC2
: FROM A a, B b
: WHERE a.id1 = b.id2
: AND a.id2 = b.id2
: UNION ALL
: SELECT a.id1, a.id2, NULL ColumnB1, NULL ColumnB2, ColumnC1, ColumnC2
: FROM A a, C c
: WHERE a.id1 = c.id2
: AND a.id2 = c.id2
: 有包子吗?

avatar
z*3
6
大牛们指教一下,没有调试过的说。。
create table d as select a.id1,a.id2,b.colb1,b.colb2,c.colc1,c.colc2 from a,b
,c where 1=0;
insert into d as select * from b;
insert into d as select * from c;
avatar
B*g
7
不对

a,b

【在 z*3 的大作中提到】
: 大牛们指教一下,没有调试过的说。。
: create table d as select a.id1,a.id2,b.colb1,b.colb2,c.colc1,c.colc2 from a,b
: ,c where 1=0;
: insert into d as select * from b;
: insert into d as select * from c;

avatar
z*3
8
vivian chow真的是不老玉女啊。。
avatar
a*8
9
- 表A的index是扫描一遍还是两遍?估计优化后是一遍。
- 如果要返回两个结果集(去掉UNION ALL一行),应该会扫描两次。有办法只扫描表A一
次吗?

【在 B*****g 的大作中提到】
: SELECT a.id1, a.id2, b.ColumnB1, b.ColumnB2, NULL ColumnC1, NULL ColumnC2
: FROM A a, B b
: WHERE a.id1 = b.id2
: AND a.id2 = b.id2
: UNION ALL
: SELECT a.id1, a.id2, NULL ColumnB1, NULL ColumnB2, ColumnC1, ColumnC2
: FROM A a, C c
: WHERE a.id1 = c.id2
: AND a.id2 = c.id2
: 有包子吗?

avatar
B*g
10
SELECT a.id1, a.id2, d.ColumnB1, d.ColumnB2, d.ColumnC1, d.ColumnC2
FROM A a,
(SELECT b.id1, b.id2, b.ColumnB1, b.ColumnB2, NULL ColumnC1, NULL ColumnC2
FROM B b
UNION ALL
SELECT c.id1, c.id2, NULL ColumnB1, NULL ColumnB2, ColumnC1, ColumnC2
FROM C c
) d
WHERE a.id1 = d.id2
AND a.id2 = d.id2

【在 a******8 的大作中提到】
: - 表A的index是扫描一遍还是两遍?估计优化后是一遍。
: - 如果要返回两个结果集(去掉UNION ALL一行),应该会扫描两次。有办法只扫描表A一
: 次吗?

avatar
d*h
11
select A.id1, A.id2, B.ColumnB1 ColumnB1, B.ColumnB2 ColumnB2, NULL as
ColumnC1, NULL as ColumnC2
from table1 A
right join table2 B
on A.id1=B.id1
and A.id2=B.id2
union all
select A.id1, A.id2, NULL as ColumnB1, NULL as ColumnB2, C.ColumnC1 ColumnC1
, C.ColumnC2 ColumnC2
from table1 A
right join table3 C
on A.id1=C.id1
and A.id2=C.id2;
avatar
b*e
12
Why you need Table A?
Table B and C is enough.
avatar
a*8
13
It's just an example for simplicity, there are other fields in table A.
avatar
B*g
14
ding

【在 a******8 的大作中提到】
: It's just an example for simplicity, there are other fields in table A.
avatar
d*h
15
select A.id1, A.id2, B.ColumnB1 ColumnB1, B.ColumnB2 ColumnB2, NULL as
ColumnC1, NULL as ColumnC2
from A
right join B
on A.id1=B.id1
and A.id2=B.id2
union all
select A.id1, A.id2, NULL as ColumnB1, NULL as ColumnB2, C.ColumnC1 ColumnC1
, C.ColumnC2 ColumnC2
from A
right join C
on A.id1=C.id1
and A.id2=C.id2;
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。