开发者

How to refer 'decider' in the where clause from the following mysql query?

How to refer 'decider' in the where clause from the followi开发者_开发百科ng mysql query?

SELECT *, 
       CASE
         WHEN (cond1) THEN 1
         WHEN (cond2) THEN 2
       END as decider
  FROM t1,
       t2 
 WHERE cond12
   AND decider <> NULL

I tried it, and I got a 1054: Unknown column in where clause error.


Use:

SELECT *, 
       CASE
         WHEN (cond1) THEN 1
         WHEN (cond2) THEN 2
         ELSE NULL
       END as decider
  FROM t1,
       t2 
 WHERE cond12
HAVING decider IS NOT NULL
  1. The earliest MySQL allows you to use column aliases is the GROUP BY clause
  2. You need to use IS NULL or IS NOT NULL (where appropriate) because NULL is not a value -- it's a placeholder for the lack of any value, which requires special handling in SQL
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜