MySQL join query using like?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 12:28:00

问题


I have been trying to get this working for quite a while now but it just doesn\'t seem to work, maybe it is is not even possible, what i am wanting to do is to perform a MySQL join query using like, such as this example i found...

SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.col LIKE \'%\' + Table2.col + \'%\'

but it just doesn\'t seem to work at all, any help that can be given would be brilliant, thanks !


回答1:


Try

SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.col LIKE CONCAT('%', Table2.col, '%')

MySQL does string concatenation differently from other databases, so in case you want to port your app, you need to have an alternate version where || is used as concatenation operator, as mentioned by Michael in another answer. This operator doesn't work in MySQL though, since it means or.




回答2:


How about this instead:

SELECT * FROM Table1, Table2 WHERE Table1.col LIKE '%'+Table2.col+'%';

Since you're selecting everything from both tables anyway, this would probably get you there.




回答3:


SELECT p.products_id, pd.products_name, p.products_model
FROM products_description pd
JOIN products p ON p.products_id = pd.products_id
WHERE pd.products_name LIKE CONCAT( '%', p.products_model, '%' )
LIMIT 0 , 100

First off you have to restrict your request by (p.products_id = pd.products_id) and LIMIT. And look what time it took. After that you can go and make comparison with (WHERE). If you gonna compare directly within JOIN you will put down your db if there are at list 20 thousands items. Beware.)



来源:https://stackoverflow.com/questions/1930809/mysql-join-query-using-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!