MYSQL设置用于连接表的临时列

时间:2018-09-05 03:03:49

标签: mysql sql

我有2个表,分别称为dataTest和custlist。 我想基于客户清单表中的“ vlookref”加入这些表, 但是在dataTest表中唯一匹配的列是'custacc'。 在dataTest表中,可以按区域对区域进行分类 例如“中部,东部,北部,南部”为半岛, “哥打基纳巴卢语,拉哈达图,桑达坎语,斗湖”为沙巴语,以及 其他人像砂拉越。 如何归档此文件,以便我可以基于客户列表中的“ vlookref”进行内部连接。Thx

dateTest模式

CREATE TABLE dataTest ( region varchar(50),custacc varchar(50));

INSERT INTO dataTest VALUES ('central','CT0135');
INSERT INTO dataTest VALUES ('eastern','CT0135');
INSERT INTO dataTest VALUES ('southern','CT0135');
INSERT INTO dataTest VALUES ('northern','CT0135');
INSERT INTO dataTest VALUES ('kota kinabalu','CT0135');
INSERT INTO dataTest VALUES ('lahad datu','CT0135');
INSERT INTO dataTest VALUES ('sandakan','CT0135');
INSERT INTO dataTest VALUES ('tawau','CT0135');
INSERT INTO dataTest VALUES ('bintulu','CT0135');
INSERT INTO dataTest VALUES ('kuching','CT0135');
INSERT INTO dataTest VALUES ('sibu','CT0135');

客户列表模式

CREATE TABLE custlist1 ( area varchar(50),vlookref varchar(50),custacc varchar(50),custname varchar(50));

INSERT INTO custlist1 VALUES ('peninsular','peninsular CT0135','CT0135','HP sdn bhd');
INSERT INTO custlist1 VALUES ('sabah','sabah CT0135','CT0135','Hup Trading sdn bhd');
INSERT INTO custlist1 VALUES ('sarawak','sarawak CT0135','CT0135','Master sdn bhd');

3 个答案:

答案 0 :(得分:0)

从读取表来看,它似乎来自Excel工作表,在这里,您可以执行查找(vlookup)的唯一方法是将两列连接在一起。在sql中不是这种情况,您应该像这样简单地加入:

from datatest
join custlist1 on custlist1.area=datatest.region 
     and custlist1.custacc=datatest.custacc

答案 1 :(得分:0)

以下查询可能会为您提供帮助:

从以下位置选择温度区域,温度custacc,客户清单1。 (

选择不同的dataTest.region

dataTest.custacc,

(当(dataTest.region ='central'或dataTest.region ='eastern'或dataTest.region ='northern'或dataTest.region ='southern'的情况)然后concat('peninsular','',dataTest .custacc)

当(dataTest.region ='kota kinabalu'或dataTest.region ='lahad datu'或dataTest.region ='sandakan'或dataTest.region ='tawau')时,则concat('sabah','',dataTest .custacc)

else concat('sarawak','',dataTest.custacc)结束)vlookrefdata      来自custlist1,dataTest

)temp JOIN custlist1 ON temp.vlookrefdata = custlist1.vlookref;

答案 2 :(得分:0)

您可以使用substring_index使用内部联接条件,

尝试类似

SELECT ct.*, dt.* 
FROM custlist1 ct INNER JOIN dataTest dt 
ON SUBSTRING_INDEX(ct.vlookref,' ',-1) = dt.custacc

SELECT ct.*, dt.* 
FROM custlist1 ct INNER JOIN dataTest dt 
ON ct.custacc = dt.custacc;

尝试这个Demo