[英]SQL Join where string is contained within another table field
I have a couple of tables that I would like to join some how. 我有几个表,我想加入一些如何。
table 1
**Name**
A Jones
J Brown
G Smith
Table 2
**fieldA**
~A Jones~G Smith~R Jones~
I want to join table 1 to 2 using the logic that where table 1.name is contained in table 2.fieldA. 我想使用表2.fieldA中包含表1.name的逻辑来连接表1到2。
therefore the result would be 因此结果将是
A Jones
G Smith
Is this possible and how? 这可能吗?怎么样?
Sorry its SQL (2008) 对不起它的SQL(2008)
This is possible , using something like this: 这是可能的 ,使用这样的东西:
SELECT name
FROM table1
LEFT JOIN table2 ON table2.fieldA LIKE CONCAT('%~', table1.name, '~%')
Is is a bad idea, though, because it will be very slow (can't use indexes) and is not a stable database design. 但是,这是一个坏主意,因为它将非常慢(不能使用索引)并且不是一个稳定的数据库设计。 You would be better off normalizing your data.
您最好将数据规范化。
This will also work for MSSQL 这也适用于MSSQL
SELECT table_1.NAME
FROM table_1
INNER JOIN table_2 ON table_2.fieldA like '%' + table_1.NAME + '%'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.