Using mysql concat() in WHERE clause?

后端 未结 7 2003
旧巷少年郎
旧巷少年郎 2020-11-29 00:59

I would like to search my table having a column of first names and a column of last names. I currently accept a search term from a field and compare it against both columns

相关标签:
7条回答
  • 2020-11-29 01:55

    Note that the search query is now case sensitive.

    When using

    SELECT * FROM table WHERE `first_name` LIKE '%$search_term%'
    

    It will match both "Larry" and "larry". With this concat_ws, it will suddenly become case sensitive!

    This can be fixed by using the following query:

    SELECT * FROM table WHERE UPPER(CONCAT_WS(' ', `first_name`, `last_name`) LIKE UPPER('%$search_term%')
    

    Edit: Note that this only works on non-binary elements. See also mynameispaulie's answer.

    0 讨论(0)
提交回复
热议问题