简体   繁体   English

从一行中的 2 个表中提取多条记录

[英]Extracting multiple records from 2 tables in one row

I need to extract datas from a first table, contening the codes, and then encode said codes with a second table.我需要从第一个表中提取数据,包含代码,然后用第二个表对所述代码进行编码。

The first table looks like:第一张表如下所示:

TABLE1表格1

ID ID Payment type付款方式 Country国家 Business Sector商业部门
00001 00001 11111111 11111111 11111111 11111111 11111111 11111111
00002 00002 22222222 22222222 22222222 22222222 22222222 22222222
00003 00003 33333333 33333333 33333333 33333333 33333333 33333333

The second table looks like第二张桌子看起来像

TABLE2表 2

ID ID Description描述 Type econde键入秒
11111111 11111111 Cash现金 Pay支付
22222222 22222222 Bank Transfer银行转帐 Pay支付
33333333 33333333 Bank Check银行支票 Pay支付
11111111 11111111 Italy意大利 Country国家
22222222 22222222 England英国 Country国家
33333333 33333333 USA美国 Country国家
11111111 11111111 First第一的 Business商业
22222222 22222222 Second第二 Business商业
33333333 33333333 Third第三 Business商业

The query I would need to extract is the following我需要提取的查询如下

ID ID Payment type付款方式 Country国家 Business Sector商业部门
00001 00001 Cash现金 Italy意大利 First第一的
00002 00002 Bank Transfer银行转帐 England英国 Second第二
00003 00003 Bank Check银行支票 USA美国 Third第三

If possible I would want to extract only 1 record selecting by the ID如果可能的话,我只想提取通过 ID 选择的 1 条记录

ID ID Payment type付款方式 Country国家 Business Sector商业部门
00002 00002 Bank Transfer银行转帐 England英国 Second第二

We can try joining the first table to the second one, thrice:我们可以尝试将第一个表连接到第二个表,三次:

SELECT
    t1.ID,
    t2a.Descritption AS "Payment type",
    t2b.Description AS Country,
    t2c.Description AS "Business Sector"
FROM TABLE1 t1
LEFT JOIN TABLE2 t2a
    ON t2a.ID = t1."Payment type" AND t2a."Type econde" = 'Pay'
LEFT JOIN TABLE2 t2b
    ON t2b.ID = t1.Country AND t2b."Type econde" = 'Country'
LEFT JOIN TABLE2 t2c
    ON t2c.ID = t1."Business Sector" AND t2c."Type econde" = 'Business';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM