0

图像。非常新,请善待。使用 DBForge 查询生成器

我有一张表,其中有许多包含缩写的列。另一个表包含缩写的描述。

需要编写一个查询来用它们的描述替换所有的缩写。尝试了许多连接都没有成功。有任何想法吗?

T1

ID  Date    Type    Cat Sub Cat
1   01/09/18    E   F   L
2   05/09/18    Cc  F   D
3   06/09/18    Cc  C   Dr
4   08/09/18    Cc  C   Sh
5   08/09/18    E   C   Sh

T2

Code    Des
E   Eft Payment
Cc  Credit Card
F   Food
C   Clothes
B   Breakfast
L   Lunch
D   Dinner
Sh  Shirt
Dr  Dress

期望的输出

ID  Date    Type D  Cat D   Sub Cat D
1   01/09/18    Eft Payment Food    Lunch
2   05/09/18    Credit Card Food    Dinner
3   06/09/18    Credit Card Clothes Dress
4   08/09/18    Credit Card Clothes Shirt
5   08/09/18    Eft Payment Clothes Shirt
4

3 回答 3

0

在下面尝试使用具有不同别名的 T2 进行多重连接

select 
   a.ID, a.date,
   b.Des as TypeD, 
   c.Des as CatD,
   d.Des as SubCatD
from T1 a inner join T2 b on a.type=b.code
inner join T2 c on a.Cat=c.code
inner join T2 d in a.SubCat=d.code
于 2018-10-18T04:42:43.983 回答
0

您需要使用 T2 表的多个实例:

select t1.id, t1.date, typ.des as type_d,ca.des as cat_d,subca.des as sub_cat_d from T1 t1 
inner join T2 typ on t1.type=typ.code 
inner join T2 ca on t1.cat = ca.code 
inner join T2 subca  on t1.subcat = subca.code;

试试这个...

于 2018-10-18T04:42:52.180 回答
0

我将假设 T1 的列是 ( ID, Date, Type, Cat, SubCat)

SELECT T1.ID, T1.Date, T2Type.Des, T2Cat.Des, T2SubCat.Des
FROM T1 INNER JOIN T2 T2Type   ON T1.Type   = T2Type.Code
        INNER JOIN T2 T2Cat    ON T1.Cat    = T2Cat.Code
        INNER JOIN T2 T2SubCat ON T1.SubCat = T2SubCat.Code
于 2018-10-18T04:47:29.817 回答