0

example pseudo-sql

SELECT *, condlabel FROM tbl WHERE cond1:(col1 = 1) OR cond2:(col2 = 2 and col3 = 4)

so the results will have additional column with exact condition label which they are satisfy

Abyss
  • 315
  • 2
  • 11
  • Why wouldn't you use `SELECT ... AS x` ? – AymDev Mar 21 '23 at 09:24
  • multiple SELECT ... AS x ? like UNION ? i think it's last case cause of multiple scans will burn out db – Abyss Mar 21 '23 at 09:26
  • 1
    No, `UNION` is used to concatenate multiple query result sets. You could just add your conditions in the `SELECT` clause like this: `col1 = 1 AS cond1`. – AymDev Mar 21 '23 at 09:28

2 Answers2

0

as AymDev commented, perfect solution is to add conditions in select block with as aliasing

Abyss
  • 315
  • 2
  • 11
0

No, you can only label (create aliased expressions) in a SELECT clause, and you cannot use those in a WHERE clause, but you can use a subquery to achieve this:

SELECT *
FROM (
   SELECT *, (col1 = 1) AS cond1, (col2 = 2 and col3 = 4) AS cond2
   FROM tbl
) AS tmp
WHERE cond1 OR cond2

Alternatively just repeat them:

SELECT *, (col1 = 1) AS cond1, (col2 = 2 and col3 = 4) AS cond2
FROM tbl
WHERE (col1 = 1) OR (col2 = 2 and col3 = 4)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375