WHERE 语句中的 MySQL AS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4611451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:13:50  来源:igfitidea点击:

MySQL AS in WHERE statement

mysql

提问by ATLChris

I have a MySQL database. I use a SELECT ASto measure "distance" between 2 points. What I want to do is use the same "distance" variable I created in the SELECTas a WHEREcondition.

我有一个 MySQL 数据库。我使用 aSELECT AS来测量两点之间的“距离”。我想要做的是使用我在 中创建的相同“距离”变量SELECT作为WHERE条件。

SELECT first_name.last_name AS name WHERE name="John Doe"

What is the best way to accomplish this?

实现这一目标的最佳方法是什么?

回答by asm

You may want to consider putting your condition in a HAVING clause rather than in the WHERE clause.

您可能需要考虑将条件放在 HAVING 子句中,而不是放在 WHERE 子句中。

See http://dev.mysql.com/doc/refman/5.5/en/select.htmlfor details on the HAVING clause.

有关HAVING 子句的详细信息,请参阅http://dev.mysql.com/doc/refman/5.5/en/select.html

回答by nate c

Column aliases can only be referenced in the ORDER BYclause (as they do not exist yet in the WHEREpart. You can do this (Although I do not necessarily recommend it as it may be slower.)

列别名只能在ORDER BY子句中引用(因为它们在WHERE部分中尚不存在。您可以这样做(尽管我不一定推荐它,因为它可能会更慢。)

select name from
(select concat(firstname, ' ', lastname) as name from yourtable)
where name = 'John Doe'

Or you could do this:

或者你可以这样做:

select (firstname, ' ', lastname) as name from yourtable
where (firstname, ' ', lastname) = 'John Doe';

The only columns you have access to in the WHEREclause are the columns in the FROMclause.

您可以在WHERE子句中访问的唯一列是子句中的列FROM

回答by Sudipta Kumar Dey

you can do one thing put the condition on where clause

你可以做一件事把条件放在 where 子句上

SELECT *,
    IF(check_out='0000-00-00 00:00:00', NOW(), check_out) AS occ_test
FROM occupied_room1
WHERE
DATE(IF(check_out='0000-00-00 00:00:00', NOW(), check_out)) = ' 2017-07-02';