简体   繁体   中英

MYSQL Set temporary columns for joining tables

I have 2 tables which is called dataTest and custlist. I want to join these tables based on the 'vlookref' in custlist tables, but the only columns that match is 'custacc' in dataTest table. From dataTest tables, the area can be classified by region for examples 'central,eastern,northern,southern' as Peninsular, 'kota kinabalu,lahad datu,sandakan,tawau' as Sabah, and others as Sarawak. How to archieve this so that I can inner join this based on 'vlookref' in custlist tables.Thx

dateTest schema

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');

custlist schema

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');

From reading your table, it appears that it comes from an Excel sheet, where the only way you could do a lookup (vlookup) was to concatenate the two columns together. This isnt the case in sql, and you should simply join like this:

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

The following query might help you:

select temp.region, temp.custacc, custlist1.* from (

select distinct dataTest.region,

dataTest.custacc,

(case when (dataTest.region = 'central' or dataTest.region = 'eastern' or dataTest.region = 'northern' or dataTest.region = 'southern' ) then concat('peninsular',' ',dataTest.custacc)

when (dataTest.region = 'kota kinabalu' or dataTest.region = 'lahad datu' or dataTest.region = 'sandakan' or dataTest.region = 'tawau' ) then concat('sabah',' ',dataTest.custacc)

else concat('sarawak',' ',dataTest.custacc) end) vlookrefdata from custlist1, dataTest

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

You can use inner join condition using substring_index,

Try something like

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

Or

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

Try this Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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