mysql amp amp 符,MySQL LIKE运算符&通配符

I've been reading tutorials and have learned nothing new. I have a table of customers. The customer's first and last names are stored in separate columns. I want to write a query that can search for customers by name, either first, last or BOTH.

Here's what I've got:

$queryyy = "

SELECT *

FROM `customers`

WHERE

`first_name1` LIKE '".mysql_real_escape_string($_GET['custname'])."%'

OR `last_name1` LIKE '%".mysql_real_escape_string($_GET['custname'])."'

AND `status` = 'active'

LIMIT 6

";

If I want to find "Lindsay Thompson", I can query for "lindsay", or for "Thompson" and get the results I want, but if I query for "lindsay thompson" I get nothing.

I feel like I'm missing the point of the wildcards, or not using them properly. Can someone please explain this to me and correct my query..

Thanks

解决方案

Wildcards are introduced to express "any number of any characters" (in case of %).

So

col LIKE '%foo'

will match for foo value and barfoo value.

What you want is actually the opposite - you need to concatenate two columns and check if it's equal to the request, like:

CONCAT(first_name, ' ', last_name) = 'foo bar'

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值