如何选择两列为一? [英] How to select two columns as one?

查看:54
本文介绍了如何选择两列为一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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.

推荐答案

您正在寻找

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.

这篇关于如何选择两列为一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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