简体   繁体   English

SQL查询内部连接与字符串中的特定数据

[英]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. 请在下面的2表中找到我要在特定条件下执行内部联接查询的位置。

Table Name:- table1 表格名称:-table1

在此处输入图片说明

Table Name:- table2 表格名称:-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. 之后的table2列Sr中 ,有一些数字类似于table1列Sr的数字。我想根据该特定数字进行内部联接。

Please guide me with which condition I will managed to do inner joint with described situation. 请指导我在描述的情况下我将设法进行内关节治疗的条件。

Working Demo from RexTester.com RexTester.com的工作演示

Assumption: table2.Name is always formatted with exactly 1 # hastag/pound sign/number sign followed by the matching digits in table 1. 假设:table2.Name的格式始终为正好1 # hastag /井号/数字符号,后跟表1中的匹配数字。

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

FROM DOCS : DOCS

SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. 在出现定界符delim之前,从字符串str返回子字符串。 If count is positive, everything to the left of the final delimiter (counting from the left) is returned. 如果count为正,则返回最后定界符左侧的所有内容(从左侧开始计数)。 If count is negative, everything to the right of the final delimiter (counting from the right) is returned. 如果count为负,则返回最后定界符右边的所有内容(从右边开始计数)。 SUBSTRING_INDEX() performs a case-sensitive match when searching for delim. 搜索delim时,SUBSTRING_INDEX()执行区分大小写的匹配。

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. 免责声明:鉴于我们在联接上使用字符串操作,因此将无法使用任何索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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