mysql - concat select 不在 where 子句中使用 (mysql)

标签 mysql concatenation

我想过滤一个表,但是表中有一个user_id列,我想在它的真实表上过滤(我的意思是users.username) .

我的代码:

SELECT SQL_CALC_FOUND_ROWS `id`, `user_id`, `payment_type`, 
 `is_paid`, `is_invoiced`, `invoice_number`, `created_at`, `ip`,
 `data`, `coupon_code`, `payment_total`, 
 (select CONCAT(users.name,' ',users.surname) from users where users.id = orders.user_id) as fullname
FROM orders
WHERE (`fullname` LIKE '%M%' OR `payment_type` LIKE '%M%') ORDER BY `is_invoiced` asc

当我执行它时,出现如下错误:Unknown column 'fullname' in 'where clause'

我该如何解决?

最佳答案

您只能在 GROUP BYORDER BYHAVING 子句中使用列别名。

来自 MySQL 文档:

Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined

引用:http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

关于mysql - concat select 不在 where 子句中使用 (mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17132611/

相关文章:

php - @variable mysql 在 php mysql 执行时返回空值

mysql - MySQL中BEGIN和END的正确使用

Javascript 原型(prototype)作用域和递归问题

vector - 许多向量的垂直串联 Mata/Stata

xslt - 使用文档功能(XSLT)时避免重命名要打开的文件的方法?

go - Golang会将文字翻译到下一行

MySql 8.0分区插件启用失败

PHP CSV 上传脚本,使用新行?

mysql - UNION 与 ORDER BY RAND() 仅适用于 MySQL 上的 LIMIT

python - pandas - 通过重复值连接列