如何从以下mysql查询的where子句中引用'decider'? [英] How to refer 'decider' in the where clause from the following mysql query?

查看:172
本文介绍了如何从以下mysql查询的where子句中引用'decider'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从以下mysql查询的where子句中引用'decider'?

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

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

我尝试了,但出现了1054: Unknown column in where clause错误.

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

推荐答案

使用:

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. 最早允许您使用列别名的MySQL是GROUP BY子句
  2. 您需要使用IS NULLIS NOT NULL(在适当情况下),因为NULL不是值-它是缺少任何值的占位符,这需要在SQL中进行特殊处理
  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

这篇关于如何从以下mysql查询的where子句中引用'decider'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆