MySQL 使用 CONCAT 条件进行选择
我正在尝试在脑海中编译这个..我有一个包含名字和姓氏字段的表 我有一个像“Bob Jones”或“Bob Michael Jones”这样的字符串以及其他几个字符串。
问题是,例如我有 鲍勃的名字,以及 迈克尔·琼斯(Michael Jones)的姓氏,
所以我正在尝试
SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast
FROM users
WHERE firstlast = "Bob Michael Jones"
,但它说未知的专栏“firstlast”..有人可以帮忙吗?
I'm trying to compile this in my mind.. i have a table with firstname and lastname fields
and i have a string like "Bob Jones" or "Bob Michael Jones" and several others.
the thing is, i have for example
Bob in firstname, and
Michael Jones in lastname
so i'm trying to
SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast
FROM users
WHERE firstlast = "Bob Michael Jones"
but it says unknown column "firstlast".. can anyone help please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您提供的别名用于查询的输出 - 它们在查询本身中不可用。
您可以重复表达式:
或包装查询
The aliases you give are for the output of the query - they are not available within the query itself.
You can either repeat the expression:
or wrap the query
试试这个:
Try this:
使用 CONCAT_WS()。
第一个参数是其余参数的分隔符。
Use CONCAT_WS().
The first argument is the separator for the rest of the arguments.
尝试:
除非您将查询作为子选择执行,否则您的别名firstlast在查询的where子句中不可用。
Try:
Your alias firstlast is not available in the where clause of the query unless you do the query as a sub-select.
除了重复 CONCAT 表达式或使用子查询之外,还有一种替代方法。您可以使用
HAVING
子句来识别列别名。这是一个有效的SQL Fiddle。
There is an alternative to repeating the
CONCAT
expression or using subqueries. You can make use of theHAVING
clause, which recognizes column aliases.Here is a working SQL Fiddle.