[英]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 ![]() |
Payment type![]() |
Country![]() |
Business Sector![]() |
---|---|---|---|
00001 ![]() |
11111111 ![]() |
11111111 ![]() |
11111111 ![]() |
00002 ![]() |
22222222 ![]() |
22222222 ![]() |
22222222 ![]() |
00003 ![]() |
33333333 ![]() |
33333333 ![]() |
33333333 ![]() |
The second table looks like第二张桌子看起来像
TABLE2表 2
ID ![]() |
Description![]() |
Type econde![]() |
---|---|---|
11111111 ![]() |
Cash![]() |
Pay![]() |
22222222 ![]() |
Bank Transfer![]() |
Pay![]() |
33333333 ![]() |
Bank Check![]() |
Pay![]() |
11111111 ![]() |
Italy![]() |
Country![]() |
22222222 ![]() |
England![]() |
Country![]() |
33333333 ![]() |
USA![]() |
Country![]() |
11111111 ![]() |
First![]() |
Business![]() |
22222222 ![]() |
Second![]() |
Business![]() |
33333333 ![]() |
Third![]() |
Business![]() |
The query I would need to extract is the following我需要提取的查询如下
ID ![]() |
Payment type![]() |
Country![]() |
Business Sector![]() |
---|---|---|---|
00001 ![]() |
Cash![]() |
Italy![]() |
First![]() |
00002 ![]() |
Bank Transfer![]() |
England![]() |
Second![]() |
00003 ![]() |
Bank Check![]() |
USA![]() |
Third![]() |
If possible I would want to extract only 1 record selecting by the ID如果可能的话,我只想提取通过 ID 选择的 1 条记录
ID ![]() |
Payment type![]() |
Country![]() |
Business Sector![]() |
---|---|---|---|
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.