简体   繁体   English

MySQL出现问题:CONCAT_WS('',name_first,name_middle,name_last),例如'%keyword%'

[英]Trouble with MySQL: CONCAT_WS(' ', name_first, name_middle, name_last) like '%keyword%'

hey folks, I'm setting up a keyword search across multiple fields: name_first, name_middle, name_last but I'm not getting the results I'd like. 大家好,我正在跨多个字段设置关键字搜索:name_first,name_middle,name_last,但是我没有得到想要的结果。 Here's the query: 这是查询:

    "SELECT accounts_users.user_ID, users.name_first, users.name_middle, users.name_last, users.company
   FROM accounts_users, users
   WHERE accounts_users.account_ID = '$account_ID' AND accounts_users.user_ID = users.id AND CONCAT_WS(' ', users.name_first, users.name_middle, users.name_last) LIKE '$user_keyword%'
   ORDER BY users.name_first ASC"

So, if I've got three names in the DB: 因此,如果我在数据库中有三个名字:

Aaron J Ban 亚伦·J·班
Aaron J Can 亚伦·J·坎
Bob L Lawblaw 鲍勃·L·劳布劳

And if the user_keyword == "bob lawblaw" I get no result. 如果user_keyword ==“ bob lawblaw”,我将不会获得任何结果。 If user_keyword == "bob L" then it returns Bob L Lawblaw. 如果user_keyword ==“ bob L”,则返回Bob L Lawblaw。 Obviously I can't force people to include the persons middle name in their keyword search but I'm stuck for the proper way to do this. 显然,我不能强迫人们在他们的关键字搜索中包括人们的中间名,但我一直坚持这样做的正确方法。

All help is greatly appreciated. 非常感谢所有帮助。

Potentially look into creating a full text index on those 3 columns and then doing a full text search on that. 可能考虑在这3列上创建全文索引,然后在其上进行全文搜索。

http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

##UPDATE## ## UPDATE ##

if using INNODB, then maybe use php's split function on the string; 如果使用INNODB,则可能在字符串上使用php的split函数;

$myVars = split(" ",$user_keyword);

then add a select statement for each word (I would also use the concat in the select statment so youu dont have to reapeat it for each where statement) 然后为每个单词添加一条选择语句(我还将在选择语句中使用concat,因此您不必为每个where语句重复使用它)

$extraSQL = '';
foreach($myVars as $search) {
    $extraSQL .= " AND `my_concat` LIKE '%".$search."%'";
}

SELECT 
    `your_fields`, 
    CONCAT_WS(' ', users.name_first, users.name_middle, users.name_last) as `my_concat` 
FROM `your_tables`
WHERE `accounts_users`.account_ID = '$account_ID' . $extraSQL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM