简体   繁体   中英

How to match column with similar but different formats from 2 MySQL tables to join them into one MySQL table?

I wanna know how to join 2 tables into one based on a column named 'Chamber_ID', however, with 2 different formats. Table A has the column with the format: SG_QRA_SGHAST_0001, 0002, etc. and Table B has the same column with format like: SGHAST.0001, 0002, etc. Does anyone know how to start this complicated task and which language should I use (Python perhaps?)? Thank you!

In case the letters are static and the only thing that changes are the numbers,
you can take a substring of the last 4 characters, which will leave you with the numbers only (with leading zeros), and then you can compare them ('0001' = '0001')

for example:

SELECT *
FROM A, B
WHERE SUBSTRING(A.Chamber_ID,-4,4) = SUBSTRING(B.Chamber_ID,-4,4)

If you prefer inner join Syntax:

SELECT *
FROM A INNER JOIN B ON SUBSTRING(A.Chamber_ID,-4,4) = SUBSTRING(B.Chamber_ID,-4,4)

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