在两个不同字段上的MySQL搜索查询 [英] MySQL Search Query on two different fields

查看:319
本文介绍了在两个不同字段上的MySQL搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用LIKE函数搜索两个字段,并且应该以相反的顺序进行匹配.我的表使用没有全文本搜索的InnoDB.

I need to search on two fields using LIKE function and should match also in reverse order. My table uses InnoDB which dont have Full text search.

请考虑以下情况:

我的用户表带有first_name和last_name列.在其上,具有以下值的行:

I have users table with first_name and last_name column. On it, there is a row with the following value:

{
    first_name: 'Ludwig',
    last_name: 'van Beethoven',
}

情况:

  1. 可以搜索路德维希·范·贝多芬"
  2. 可以搜索贝多芬·路德维希"
  3. 可以搜索路德维希"
  4. 可以搜索贝多芬"

我尝试了这个SQL语句,但是没有运气.

I tried this SQL statement but no luck.

SELECT CONCAT(first_name, ' ', last_name) as fullname 
  FROM users 
 WHERE fullname LIKE '%Ludwig van Beethoven%';

推荐答案

您需要在where子句中重新声明concat表达式.

You need to re-state the concat expression in your where clause.

 SELECT CONCAT(first_name, ' ', last_name) as fullname 
      FROM users 
     WHERE CONCAT(first_name, ' ', last_name) LIKE '%doe%';

不幸的是,"as"只是创建列别名,而不是您可以在其他地方使用的变量.

Unfortunately "as" just create a column alias, not a variable that you can use elsewhere.

这篇关于在两个不同字段上的MySQL搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆