mysql having and or_在MySQL的HAVING和WHERE子句之间使用“ OR”?

bd96500e110b49cbb3cd949968f18be7.png

I am trying to fetch records in MySQL using a simple used submitted field. More precisely, the user inputs a name (firstname or lastname or fullname) and the server should return matched rows.

What I am doing so far is something like:

SELECT * FROM people

WHERE

firstname LIKE '%user_submitted_data%' OR

lastname LIKE '%user_submitted_data%'

That works well for now, but that (obviously) won't work when a user submits the fullname. Is there a way to add a OR between the whole 'WHERE type conditions' and the 'HAVING type conditions'? This way I could do something like:

SELECT [some fields], CONCAT(firstname, ' ', 'lastname') as fullname

FROM people

WHERE

firstname LIKE '%user_submitted_data%' OR

lastname LIKE '%user_submitted_data%' OR

HAVING fullname LIKE '%user_submitted_data%'

I know I could just split the original string but that has some negative impact since you have to deal with names containing spaces such as 'De Gaule' and stuff like that.

解决方案

Do a subquery:

SELECT [some fields]

FROM

SELECT firstname, lastname, CONCAT(firstname, ' ', lastname) as fullname

FROM people) AS tmp

WHERE firstname LIKE '%user_submitted_data%'

OR lastname LIKE '%user_submitted_data%'

OR fullname LIKE '%user_submitted_data%'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值