Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL的where子句中使用别名?_Mysql_Sql - Fatal编程技术网

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

如何在MySQL的where子句中使用别名?,mysql,sql,Mysql,Sql,如何在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%' O

如何在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
子句是在
where
子句之后计算的(此时没有别名)。但是,您可以使用
having
子句,或者使用临时表。

实际上不能,因为
select
子句是在
where
子句之后计算的(在该点上没有别名)。但是,您可以使用
having
子句,也可以使用临时表。

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

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
子句。一个用于子查询中的列条件,另一个用于外部查询中的列别名条件。例如:

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

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

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
子句。一个用于子查询中的列条件,另一个用于外部查询中的列别名条件。例如:

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

在where子句之后,我也使用having子句,但是………在这种情况下,仅检查having子句条件……。我可以在这方面做些什么?请详细说明……因为在使用having子句时,我不能过滤多个记录clause@mahes:将
where
更改为
having
……在这种情况下,只检查having子句条件……。我可以在这方面做些什么?请详细说明……因为在使用having时,我不能过滤多个记录clause@mahes:将
where
更改为
having
。在连接的情况下,我们可以使用与上述相同的策略,即将连接作为派生表,将列别名条件移动到外部WHERE子句。如果要连接我们可以使用的策略如上所述,即将连接作为派生表,请将列别名条件移动到外部WHERE子句。