|
查看: 1628|回复: 9
|
SQL Command
[复制链接]
|
|
|
发表于 4-2-2009 06:47 PM
|
显示全部楼层
这是我的方法:
declare @table table(Custid Varchar(10), QtyA INT,Paper1 Varchar(5),Paper2 Varchar(5),Number INT,QtyB INT)
insert into @table(CustID, QtyA, Paper1, Paper2)
select CustID, QtyA, Paper1, Paper2 from TableA
insert into @table(Custid, Number,QtyB)
select Custid, Number,QtyB from TableB
SELECT * FROM @table |
|
|
|
|
|
|
|
|
|
|
TableA
Custid QtyA Paper1 Paper2
A 2 aa bb
A 3 aa bb
TableB
Custid Number QtyB
A 1 5
A 2 7
A 3 9
TableC
Custid QtyA Paper1 Paper2 Number QtyB
A 2 aa bb NULL NULL
A 3 aa bb NULL NULL
A NULL NULL NULL 1 5
A NULL NULL NULL 2 7
A NULL NULL NULL 3 9
我想问有没有任何SQL command 能把一上的TableA join TableB 然后变成TableC?? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 5-2-2009 10:34 AM
|
显示全部楼层
原帖由 大傻姐 于 4-2-2009 06:47 PM 发表 
这是我的方法:
declare @table table(Custid Varchar(10), QtyA INT,Paper1 Varchar(5),Paper2 Varchar(5),Number INT,QtyB INT)
insert into @table(CustID, QtyA, Paper1, Paper2)
select CustID, QtyA, ...
谢谢。。。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 5-2-2009 10:42 AM
|
显示全部楼层
Table A
custid name
a aa
Table B
number paper custid
123456 1 a
456789 2 a
987654 3 a
Table C
part custid qty
abc123 a 12
def456 a 13
Table D
custid name number paper part qty
a aa 123456 1 abc123 12
a aa 456789 2 def456 13
a aa 987654 3 NULL NULL
那以上的把Table A, Table B, Table C join to Table D有可能吗?? |
|
|
|
|
|
|
|
|
|
|
发表于 5-2-2009 11:53 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 5-2-2009 03:48 PM
|
显示全部楼层
我也知道,但是出来的结果不是我想要的。。。会重复的 |
|
|
|
|
|
|
|
|
|
|
发表于 6-2-2009 10:44 AM
|
显示全部楼层
应该是不可能吧
当Table A join Table B,就会有
Table A_B
custid name number paper
a aa 123456 1
a aa 456789 2
a aa 987654 3
你这么确定Table C的第一个Row是要放在Table A_B的第一个row呢?since它们是join by custID, Table A_B 三个row 的custID 都一样。
除非你的Table C也有column paper 或 number, 那就可以这样:
SELECT A.custid, A.name, B.number, B.paper,
(SELECT Part FROM Table C Where custID = A.custID and number = B.Number and paper = B.paper) AS Part,
(SELECT Qty FROM Table C Where custID = A.custID and number = B.Number and paper = B.paper) AS Qty
FROM TableA A INNER JOIN TableB B ON A.custID = B.custID |
|
|
|
|
|
|
|
|
|
|
发表于 16-2-2009 10:18 AM
|
显示全部楼层
CREATE VIEW VIEWNAME
AS
SQL COMMAND |
|
|
|
|
|
|
|
|
|
|
发表于 24-2-2009 05:09 PM
|
显示全部楼层
好笑,table A join Table B 之后, 跟Table C 完全link不起来
除非table C 有 paper / number
基本上你两个问题都是可以用 full join 解决,
先决条件是, table structure要对.
重不重复,是看你join 到对不对.
[ 本帖最后由 FiberOptic 于 24-2-2009 05:11 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 25-2-2009 02:55 PM
|
显示全部楼层
回复 1# 奋斗1314 的帖子
select Custid, QtyA,Paper1,Paper2 ,null as Number, null as QtyB from tableA
UNION
select null as Custid, null as QtyA,null as Paper1,null as Paper2 ,Number, QtyB from tableB |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|