求SQL语句,表A一条数据对表B多条数据, 根据表B的状态等关联表A,根据表A每个项目生成一条数据
表Product产品表IDProductName1苹果2香蕉3梨4菠萝表Invoice状态表(一个产品对应多条状态)IDProduceID(Product表ID)Date...
表Product 产品表
ID ProductName
1 苹果
2 香蕉
3 梨
4 菠萝
表Invoice 状态表(一个产品对应多条状态)
ID ProduceID(Product表ID) Date(日期) status(是否到货)
1 1 2013-01-01 receive
2 1 2013-01-02 execute
3 1 2013-01-03 finish
4 2 2013-01-04 receive
5 2 2013-01-05 execute
6 3 2013-01-01 receive
7 4 2013-01-01 receive
8 4 2013-01-03 execute
9 5 2013-01-05 finish
要求生成这样的结果:
ID Name status1 date1 status2 date2 status3 date3
1 苹果 receive 2013-01-01 execute 2013-01-02 finish 2013-01-03
2 香蕉 receive 2013-01-04 execute 2013-01-05 NULL NULL
3 梨 receive 2013-01-01 NULL NULL NULL NULL
4 菠萝 receive 2013-01-01 execute 2013-01-03 finish 2013-01-05 展开
ID ProductName
1 苹果
2 香蕉
3 梨
4 菠萝
表Invoice 状态表(一个产品对应多条状态)
ID ProduceID(Product表ID) Date(日期) status(是否到货)
1 1 2013-01-01 receive
2 1 2013-01-02 execute
3 1 2013-01-03 finish
4 2 2013-01-04 receive
5 2 2013-01-05 execute
6 3 2013-01-01 receive
7 4 2013-01-01 receive
8 4 2013-01-03 execute
9 5 2013-01-05 finish
要求生成这样的结果:
ID Name status1 date1 status2 date2 status3 date3
1 苹果 receive 2013-01-01 execute 2013-01-02 finish 2013-01-03
2 香蕉 receive 2013-01-04 execute 2013-01-05 NULL NULL
3 梨 receive 2013-01-01 NULL NULL NULL NULL
4 菠萝 receive 2013-01-01 execute 2013-01-03 finish 2013-01-05 展开
1个回答
展开全部
Select
A.ID,
A.ProductName as Name,
B.stauts as status1,
B.Date as data1,
C.stauts as status2,
C.Date as data2,
D.stauts as status3,
D.Date as data3
From
Product A
Left Join Invoice B On A.ProduceID=B.ID AND B.status='receive'
Left Join Invoice C On A.ProduceID=C.ID AND C.status='execute'
Left Join Invoice D On A.ProduceID=D.ID AND D.status='finish'
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |