php – 在SQL中为WHERE子句组合两列

前端之家收集整理的这篇文章主要介绍了php – 在SQL中为WHERE子句组合两列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的sql中,我使用WHERE和LIKE子句执行搜索.但是,我需要使用两列的组合值来执行搜索:first_name和last_name:

WHERE customers.first_name customers.last_name LIKE’%John Smith%’

这不行,但是我想知道我该如何做这些事情呢?

我试图用两列分开搜索,就像这样:

WHERE customers.first_name LIKE’%John Smith%’OR customers.last_name LIKE’%John Smith%’

但显然这将无法正常工作,因为搜索查询是这两列的组合值.

使用以下内容
WHERE CONCAT(customers.first_name,' ',customers.last_name) LIKE '%John Smith%'

请注意,为了使其按预期工作,应修剪名字和姓氏,即不应包含前导或尾随空格.在插入数据库之前,最好在PHP中修剪字符串.但您也可以将修剪结合到您的查询中:

WHERE CONCAT(TRIM(customers.first_name),TRIM(customers.last_name)) LIKE '%John Smith%'
原文链接:https://www.f2er.com/php/139854.html

猜你在找的PHP相关文章