[英]How can I use alias name in where clause in MySQL?
how can i use alias name in where clause in mysql 我如何在mysql的where子句中使用别名
SELECT
*,
CASE roles.rol_active
WHEN '1' THEN 'yes'
WHEN '0' THEN 'no'
END AS roles_active
FROM
roles
WHERE
rol_is_deleted =
AND (rol_name LIKE '%ac%'
OR rol_display_name LIKE '%ac%'
OR rol_description LIKE '%ac%'
OR rol_active LIKE '%ac%'
OR rol_updated_by LIKE '%ac%'
OR rol_updated_at LIKE '%ac%')
ORDER BY rol_name asc
LIMIT 10 OFFSET 0;
You actually can't, because select
clause is evaluated after the where
clause (there are no aliases at that point). 您实际上不能这样做,因为
select
子句在where
子句之后进行求值(此时没有别名)。 However, you can use a having
clause instead or use a temporary table. 但是,您可以改用
having
子句或使用临时表。
Wrap a part of your current query up in a derived table: 将当前查询的一部分包装在派生表中:
select * from
(
SELECT
*,
CASE roles.rol_active
WHEN '1' THEN 'yes'
WHEN '0' THEN 'no'
END AS roles_active
FROM
roles
) as dt
WHERE
rol_is_deleted =
AND (rol_name LIKE '%ac%'
OR rol_display_name LIKE '%ac%'
OR rol_description LIKE '%ac%'
OR rol_active LIKE '%ac%'
OR rol_updated_by LIKE '%ac%'
OR rol_updated_at LIKE '%ac%')
ORDER BY rol_name asc
LIMIT 10 OFFSET 0;
You can even have two separate WHERE
clauses. 您甚至可以拥有两个单独的
WHERE
子句。 One for column conditions in the sub-query, and another one for column alias conditions in the outer query. 一个用于子查询中的列条件,另一个用于外部查询中的列别名条件。 Ie something like:
即类似:
...
WHERE column-conditions
) as dt
WHERE column-alias-conditions
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.