简体   繁体   中英

Sql query Inner join with specific data in string

Please find my below 2 table's where i want to execute inner join query with the specific condition.

Table Name:- table1

在此处输入图片说明

Table Name:- table2

在此处输入图片说明

In table2 column Sr after # there are some number's which is similar to table1 column Sr number's.I want to do inner join as per that particular number.

Please guide me with which condition I will managed to do inner joint with described situation.

Working Demo from RexTester.com

Assumption: table2.Name is always formatted with exactly 1 # hastag/pound sign/number sign followed by the matching digits in table 1.

SELECT T1.Name, T2.value
FROM table2 T2
INNER JOIN table1 T1
 on T1.SR = substring_index(T2.Name,'#',-1);

FROM DOCS :

SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
'www.mysql'
SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
'mysql.com'

Disclaimer: Given we are using string manipulation on a join, no index will be able to be used.

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