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
MySQL AS in WHERE statement
提问by ATLChris
I have a MySQL database. I use a SELECT AS
to measure "distance" between 2 points. What I want to do is use the same "distance" variable I created in the SELECT
as a WHERE
condition.
我有一个 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 BY
clause (as they do not exist yet in the WHERE
part. 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 WHERE
clause are the columns in the FROM
clause.
您可以在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';