sql查询联接两个表 [英] sql query to join two tables
问题描述
hi ..
我有个问题.请帮我解决.
我有两个表1ST结构是
ID HEAD1 HEAD2 HEAD3
10 3400 500 600
11 6400 800 600
12 8400 500 600
131400900600
-----------------------------
第二张表
标题说明
HEAD1 AAAAA
HEAD2 BBBBB
HEAD3 CCCCC
HEAD4 DDDDD
-------------------------------------------------
我必须通过其描述来获取价值. AS:---
ID说明AMT
10 AAAAA 3400
10 BBBBB 500
10 CCCCC 600
11 AAAAA 6400
11 BBBBB 800
11 CCCCC 600
12 AAAAA 6400
12 BBBBB 800
12 CCCCC 600
13 AAAAAA 1400
13 BBBBBB 900
13 CCCCCC 600
hi..
I have a problem. please help me in solving it.
I HAVE TWO TABLE 1ST STRUCTURE IS
ID HEAD1 HEAD2 HEAD3
10 3400 500 600
11 6400 800 600
12 8400 500 600
13 1400 900 600
-----------------------------
2ND TABLE IS
HEADNAME DESCRIPTION
HEAD1 AAAAA
HEAD2 BBBBB
HEAD3 CCCCC
HEAD4 DDDDD
-------------------------------------------------
I HAVE TO GET VALUE WITH THEIR DESCRIPTION. AS :---
ID DESCRIPTION AMT
10 AAAAA 3400
10 BBBBB 500
10 CCCCC 600
11 AAAAA 6400
11 BBBBB 800
11 CCCCC 600
12 AAAAA 6400
12 BBBBB 800
12 CCCCC 600
13 AAAAAA 1400
13 BBBBBB 900
13 CCCCCC 600
推荐答案
要联接至少一列,在两个表中应该是公用的,但是在您的表中没有公用的列,请告诉我如何获取金额,因为AMT列在任何表中都不存在.
For joining at least one column should be common in both table but in your table there is no common column and tell me how can u get amount because AMT column does not exist in any table .
您对数据库的设计不够好
但按照输入输出示例,
您可以使用以下查询,
you have not designed database very well
but as per input output example,
you can use below query,
select * from
(
select Id,(select Description from table2 where headname='head1') as Description,head1 as amt
from table1
union all
select Id,(select Description from table2 where headname='head2') as Description,head2 as amt
from table1
union all
select Id,(select Description from table2 where headname='head3') as Description,head3 as amt
from table1
) as a
order by id,Description
祝您编程愉快!
:)
Happy coding!
:)
这篇关于sql查询联接两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!