在选择MYSQL时不能使用过滤器到哪里去虚拟列

时间:2013-09-22 08:34:58

标签: mysql sql

我将此查询与虚拟列New_Users

一起使用
SELECT `r`.`deal_id` as Deal_ID, 
       concat("2013\-05\-15") as start_date, 
       concat("2013\-05\-16") as end_date, 
       abs(0) AS New_Members 
  FROM (`ws_deal` r) 
 WHERE New_Members != 0 
 ORDER BY `deal_id`" 

我在'where子句'SQL.sql 1 118“中出现错误”1 Unknown column'New_Members'

如果我没有使用New_Members != 0并且查询是

 SELECT `r`.`deal_id` as Deal_ID, 
        concat("2013\-05\-15") as start_date, 
        concat("2013\-05\-16") as end_date, 
        abs(0) AS New_Members 
   FROM (`ws_deal` r)  
  ORDER BY `deal_id` 
  LIMIT 12"  

我得到了一个结果。

Deal_ID start_date      end_date       New_Members  

407 2013-05-15  2013-05-16  0
408 2013-05-15  2013-05-16  0
409 2013-05-15  2013-05-16  0
410 2013-05-15  2013-05-16  0
411 2013-05-15  2013-05-16  0
412 2013-05-15  2013-05-16  0
413 2013-05-15  2013-05-16  0
414 2013-05-15  2013-05-16  0
415 2013-05-15  2013-05-16  0
416 2013-05-15  2013-05-16  0
417 2013-05-15  2013-05-16  0

我的问题是为什么我无法过滤此结果。我该如何过滤这个。 (您可能认为无论如何New_Member != 0并且不需要过滤。但我需要通过过滤器来实现这一点,而这是在大型查询集中动态生成的查询)

非常感谢

2 个答案:

答案 0 :(得分:0)

您不能在where子句中使用别名列,但您可以使用子查询并按新列过滤:

select
    Deal_ID, start_date, end_date, New_Members
from (
    select
        `r`.`deal_id` as Deal_ID, 
        concat("2013\-05\-15") as start_date, 
        concat("2013\-05\-16") as end_date, 
        abs(0) AS New_Members 
    from (`ws_deal` r) 
) as a
where New_Members != 0 
order by `deal_id`" 

答案 1 :(得分:0)

首先,abs(0)没有意义。它将始终返回0.

其次,您不能在where子句中使用别名。您必须在查询中查询。