我试图做一个类似搜索页面的功能。我想查询以查找“ ColumnA”和“ ColumnB”,“ ColumnC”和“ ColumnD”中的“查询”。并选择在任何这些列中具有单词/词组“ query”的行。这似乎起作用:
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
但这很冗长。有没有更简单或更有效的方式来做到这一点?
简单的解决方法:
SELECT * FROM projects WHERE CONCAT(category,name,description,keywords,type) LIKE '%query%' ORDER BY name ASC;
您可以根据需要在各列之间添加分隔符:
SELECT * FROM projects WHERE CONCAT(category,"|",name,"|",description,"|",keywords,"|",type) LIKE '%query%' ORDER BY name ASC;
您还可以使用全文本搜索(您需要按此处所述创建全文本索引:多列上的FULLTEXT INDEXES如何工作?)
SELECT *, MATCH (category,name,description,keywords,type) AGAINST ('query') AS score FROM projects WHERE MATCH (category,name,description,keywords,type) AGAINST ('query');