如何选择两列为一?

编程入门 行业动态 更新时间:2024-10-24 06:33:45
本文介绍了如何选择两列为一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用PHP从MySQL数据库中搜索两个字段.

I'm trying to search two fields as one from a MySQL database using PHP.

例如

mysql_query(" SELECT (first_name,last_name) As name FROM people WHERE (name LIKE '%" . $term . "%') ");

我认为这是要使用的代码,但无济于事.自从我这样做已经有一段时间了,我不记得确切地如何获得期望的结果.

I thought that this was the code to use, but to no avail. It has been a while since I've done this and I can't remember exactly how to achieve the desired result.

推荐答案

您正在寻找 CONCAT 函数.

You're looking for the CONCAT function.

mysql_query("SELECT CONCAT(first_name, last_name) As name FROM people WHERE (CONCAT(first_name, last_name) LIKE '%" . $term . "%')");

甚至...

mysql_query("SELECT CONCAT(first_name, ' ', last_name) As name FROM people WHERE (CONCAT(first_name, ' ', last_name) LIKE '%" . $term . "%')");

我无法向您解释其背后的原因(...但是也许有人可以发表评论?),但是您不能使用name别名来搜索这两个字段,您必须明确地

I couldn't explain you the reasons behind this (...but maybe someone can leave a comment?), but you can't use the name alias to search for both fields, you have to explicitly CONCAT again.

更多推荐

如何选择两列为一?

本文发布于:2023-10-24 13:45:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何选择

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!