我想过滤一个表,但是表中有一个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 BY
、ORDER BY
或 HAVING
子句中使用列别名。
来自 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/