2015-02-19 5 views
0

Я пытаюсь объединить два разных столбца внутри таблиц, используя строки, которые являются лишь частично общими между двумя столбцами двух разных таблиц.(string) LIKE query with join in Mysql

Как объединить эти две таблицы:

Предположим, если у меня есть 2 таблицы, table1, TABLE2

╔════╦══════════════════════════════════╦══════╗ 
║ T1 ║ String1       ║ snr ║ 
╠════╬══════════════════════════════════╬══════╣ 
║ 1 ║ Jeff Atwood is good but naughty ║ 5636 ║ 
║ 2 ║ Geoff Dalgas is bully and fat ║ 148 ║ 
║ 3 ║ Jeff Atwood likes skoda and hate ║  ║ 
║ ║ ferrari       ║ 101 ║ 
║ 4 ║ Geoff Dalgas is smart but not ║  ║ 
║ ║ intelligent      ║ 959 ║ 
╚════╩══════════════════════════════════╩══════╝ 

╔════╦══════════════════════════════════╦══════╗ 
║ T2 ║ String2       ║ bnr ║ 
╠════╬══════════════════════════════════╬══════╣ 
║ 5 ║ Jeff Atwood is good    ║ 1323 ║ 
║ 34║ Geoff Dalgas is bully   ║12148 ║ 
║ 73║ Jeff Atwood likes skoda   ║26101 ║ 
║ 64║ Geoff Dalgas is smart but  ║56959 ║ 
╚════╩══════════════════════════════════╩══════╝ 

Это то, что я пытаюсь для того чтобы достигнуть

Результат:

╔════╦══════════════════════════════════╦══════╦══════╦══════╗ 
║ T1 ║ String1       ║ snr ║bnr ║T2 ║ 
╠════╬══════════════════════════════════╬══════╬══════╬══════╣ 
║ 1 ║ Jeff Atwood is good but naughty ║ 5636 ║1323 ║5  ║ 
║ 2 ║ Geoff Dalgas is bully and fat ║ 148 ║12148 ║34 ║ 
║ 3 ║ Jeff Atwood likes skoda and hate ║  ║  ║  ║ 
║ ║ ferrari       ║ 101 ║26101 ║73 ║ 
║ 4 ║ Geoff Dalgas is smart but not ║  ║  ║  ║ 
║ ║ intelligent      ║ 959 ║56959 ║64 ║ 
╚════╩══════════════════════════════════╩══════╩══════╩══════╝ 

Единственное отношение, которое я вижу, это сравнить строку1 и строку2 (которая является pa rtially равно)

Это мой синтаксис:

SELECT table1.T1, table1.String1, table1.snr, table2.bnr,table2.T2 FROM table1 INNER JOIN table2 WHERE table1.string1 LIKE table2.string2 

но я получить любую ошибку,

There is a chance that you may have found a bug in the SQL parser. Please examine your query closely, and check that the quotes are correct and not mis-matched. Other possible failure causes may be that you are uploading a file with binary outside of a quoted text area. You can also try your query on the MySQL command line interface. The MySQL server error output below, if there is any, may also help you in diagnosing the problem. If you still have problems or if the parser fails where the command line interface succeeds, please reduce your SQL query input to the single query that causes problems, and submit a bug report with the data chunk in the CUT section below:

ERROR: C1 C2 LEN: 56 57 770 STR: etc...

ответ

0

Если у вас нет каких-либо символов подстановки в LIKE аргумент, он просто делает точное совпадение строк. Вам нужно добавить % символы:

SELECT table1.T1, table1.String1, table1.snr, table2.bnr,table2.T2 
FROM table1 
INNER JOIN table2 ON table1.string1 LIKE CONCAT('%', table2.string2, '%') 
+0

хорошо я попытался с вышесказанным, но он вернулся пустой набор, позвольте мне написать в SQL Fiddle и посмотреть, если он работает –

+0

Он работает. . !!!! http://sqlfiddle.com/#!2/1febf/1 –

-1

Попробуйте это:

Select ID, String From (
(SELECT t1, string1 FROM table1) 
UNION 
(SELECT t2,string2 FROM table2)) order by ID; 
+0

Это не ответит на ваш вопрос. Я просматривал его на мобильном телефоне, поэтому пропустил многие вопросы. –

+0

(1) выход не имеет желаемых 5 столбцов; (2), который возвращает 8 строк; 4. –