Create View V_A AsSelect T.*, (Select Count(*) From 表A T0 Where t0.单号A=T.单号A And T0.序号A<=T.序号A) as Index2From 表A TCreate View V_B AsSelect T.*, (Select Count(*) From 表B T0 Where t0.单号B=T.单号B And T0.序号B<=T.序号B) as Index2From 表B TSelect *From V_A Left Join V_B On a.单号A=B.序号B And A.Index2 = B.Index2
序号是没问题了,但A,B表的内容还有这种情况单号A 序号A 名称A 单号B 序号B 名称B001 1 C 001 1 KK001 2 D 001 7 SS003 1 H 002 1 C 如何查询得到以下内容单号A 序号A 名称A 单号B 序号B 名称B001 1 C 001 1 KK001 2 D 001 7 SS 002 1 C 003 1 H
Select *From V_A Full Join V_B On a.单号A=B.序号B And A.Index2 = B.Index2 或者Select A.*,B.*From V_A a Left Join V_B b On a.单号A=B.序号B And A.Index2 = B.Index2 Union AllSelect A.*,B.*From V_B b Left Join V_A a On a.单号A=B.序号B And A.Index2 = B.Index2 Where A.单号 is Null