MySQL MySQL连接查询使用like?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1930809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL join query using like?
提问by David
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...
我已经尝试让它工作了很长一段时间,但它似乎不起作用,也许它甚至不可能,我想要做的是使用类似的方式执行 MySQL 连接查询,例如我发现的例子...
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 !
但它似乎根本不起作用,任何可以提供的帮助都会很棒,谢谢!
回答by Tor Valamo
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
.
MySQL 的字符串连接与其他数据库不同,因此如果您想移植应用程序,您需要有一个替代版本||
用作连接运算符,正如 Michael 在另一个答案中提到的那样。但是这个运算符在 MySQL 中不起作用,因为它意味着or
.
回答by inkedmn
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.
由于您无论如何都要从两个表中选择所有内容,因此这可能会让您到达那里。
回答by Александр Тарасов
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.)
首先,您必须通过 (p.products_id = pd.products_id) 和 LIMIT 来限制您的请求。看看花了多长时间。之后你可以去和(WHERE)进行比较。如果您要直接在 JOIN 中进行比较,如果列表中有 2 万个项目,您将放下您的数据库。谨防。)