日期:2014-05-18  浏览次数:20704 次

请教一段SQL(在线等)谢谢!
此SQL涉及4个表:A货品资料,B客户的货品资料,C订单单头,D订单单体。
大致字段如下(字段名相同即可关联):
A:货号、货名;
B:货号、客户货号、客户号;
C:订单号、客户号;
D:订单号、货号。

我写了段SQL想查出如下内容:A.货名、B客户货号、D、订单号,但一直不成功,请教大家怎么实现?


                           



------解决方案--------------------
select A.货名,B.客户货号,D.订单号 from A inner join B on A.货号=B.货号
inner join D on A.货号=D.货号
------解决方案--------------------
select A.货名,B.客户货号,D.订单号
from A left inner join B on A.货号=B.货号
left inner join D on A.货号=D.货号
------解决方案--------------------
select A.货名,B.客户货号,D.订单号 from a
inner join b on a.货号=b.货号
inner join c on c.客户号=b.客户号
inner join d on d.订单号=c.订单号
------解决方案--------------------
select A.货名,B.客户货号,D.订单号
from A,B,C,D
where A.货号=B.货号
and B.货号=D.货号
and C.订单号=D.订单号

------解决方案--------------------
A:货号、货名;
B:货号、客户货号、客户号;
C:订单号、客户号;(头)
D:订单号、货号。 (身)

select a.订单号,c.客户货号,d.货名 from C a left join D b on a.订单号=b.订单号
left join B c on a.客户号=c.客户号
and b. 货号=c.货号
left join A d on b.货号=d.货号

这应该是你要的
------解决方案--------------------
如果有的客户没有指定的“客户货号”,建议这样写
select A.货名,
(select B.客户货号
from B
where B.货号=D.货号
and C.客户号=B.客户号)客户货号,D.订单号
from A,C,D
where A.货号=D.货号
and C.订单号=D.订单号

------解决方案--------------------

select A.货名,B.客户货号,D.订单号 from a left join b on a.货号=b.货号 left join c on c.客户号=b.客户号 left join d on d.订单号=c.订单号
------解决方案--------------------

select A.货名,B客户货号,D.订单号
from B left outer join A on b.货号=a.货号 left outer join c on b.客户号=c.客户号
inner join d on a.货号=d.货号 and c.订单号=d.订单号