gpt4 book ai didi

mysql - 如何在 MySQL 的 where 子句中使用别名?

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:21 24 4
gpt4 key购买 nike

如何在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;

最佳答案

将当前查询的一部分包装在派生表中:

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;

您甚至可以有两个单独的 WHERE 子句。一个用于子查询中的列条件,另一个用于外部查询中的列别名条件。 IE。像这样的东西:

...
WHERE column-conditions
) as dt
WHERE column-alias-conditions
...

关于mysql - 如何在 MySQL 的 where 子句中使用别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33917159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com