MySQL:在多列中查找相同的字符串 [英] MySQL: Look for the same string in multiple columns
本文介绍了MySQL:在多列中查找相同的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试提供类似搜索页面的功能.我想查询以查找"ColumnA"和"ColumnB","ColumnC"和"ColumnD"中的查询".并选择在任何这些列中具有单词/短语"query"的行. 看来可行:
I am trying to make a search-page-like function. I want to make a query to look for "query" in "ColumnA" and "ColumnB" and "ColumnC" and "ColumnD". And select the rows which has the word/phrase "query" in any of those columns. This appears to work:
SELECT * FROM projects
WHERE
category LIKE '%query%' OR
name LIKE '%query%' OR
description LIKE '%query%'OR
keywords LIKE '%query%' OR
'type' LIKE '%query%'
ORDER BY name ASC
但是很长.有没有更简单或更有效的方法?
But it is lengthy. Is there any easier or more efficient way of doing this?
推荐答案
简单的解决方法:
SELECT *
FROM projects
WHERE
CONCAT(category,name,description,keywords,type) LIKE '%query%'
ORDER BY name ASC;
如果需要,您可以在列之间添加分隔符:
You can add separators between columns if needed:
SELECT *
FROM projects
WHERE
CONCAT(category,"|",name,"|",description,"|",keywords,"|",type) LIKE '%query%'
ORDER BY name ASC;
You can also use a fulltext search (you need to create a fulltext index as described here: How do FULLTEXT INDEXES on multiple columns work?)
SELECT *, MATCH (category,name,description,keywords,type) AGAINST ('query') AS score FROM projects WHERE MATCH (category,name,description,keywords,type) AGAINST ('query');
这篇关于MySQL:在多列中查找相同的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文