简体   繁体   中英

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.

therefore the result would be

A Jones
G Smith

Is this possible and how?

Sorry its 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

SELECT table_1.NAME
FROM table_1
INNER JOIN table_2 ON table_2.fieldA like '%' + table_1.NAME  + '%'

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