How can I pass a "solver" to a where clause from the following mysql query?

How to pass 'decider' to 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

      

I tried and I got an error 1054: Unknown column in where clause

.

+1


source to share


1 answer


Using:

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

      



  • The earliest MySQL allows column aliases - this is a suggestion GROUP BY

  • You need to use IS NULL

    or IS NOT NULL

    (optionally) because it is NULL

    not a value - it is a placeholder for missing any value that requires special handling in SQL
+1


source







All Articles