我有一个查询

SELECT DISTINCT SQL_CALC_FOUND_ROWS cl.id AS id,
cl.column1 AS column1,
cl.column2 AS column2,
cl.column3 AS column3,
cl.t_sc_1 AS t_sc_1,
cl.t_sc_2 AS t_sc_2,
cl.t_sc_3 AS t_sc_3
FROM classifieds AS cl
WHERE (cl.t_sc_1 = 1 OR cl.t_sc_2 = 1 OR cl.t_sc_3 = 1)
AND column1 = 10 AND column2=10 AND column3=10
ORDER BY cl.id ASC


我想获得结果是否(t_sc_1 = 1或t_sc_2 = 1或t_sc_3 = 1)或the_three_columns = 1 AND column1 = 10等等,...和column1 = 10等等。
因此,仅t_sc_1 = 1的行或仅column3 = 1的行不会显示在结果中。

真正的查询是动态的,有时“ OR”部分包含两个cl.t_sc_1 = 1(例如(cl.t_sc_1 = 1 OR cl.t_sc_2 = 1)),有时包括三个,如示例中所示。

我知道子句是否不存在,但是有办法实现吗?

谢谢您的帮助

最佳答案

打电话给

column1 = 1   as  a
column2 = 1   as  b
column3 = 1   as  c
column4 = 10  as  d


您说的是:

(a + b + c) + (a.b.c.d)
= a + b + c + a.b.c.d


使用布尔代数的等效于:

a + b + c.(1 + a.b.d)
= a + b + c.1
= a + b + c


的意思是:

where (
    column1 = 1
    or column2 = 1
    or column3 = 1
    )
or (
    column1 = 1
    and column2 = 1
    and column3 = 1
    and column4 = 10
    )


等效于:

where column1 = 1
    or column2 = 1
    or column3 = 1


要么:

where 1 in (column1, column2, column3);

08-08 08:34