我有一个像
这样的查询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等等,......我得到的结果是t_sc_1 = 1和t_sc_2 = 1而t_sc_3 = 1和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))有时包括示例中的三个。
我知道条款是否不存在但是有没有办法实现这个目标?
感谢您的帮助
答案 0 :(得分:2)
让我们来电话
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);
答案 1 :(得分:1)
您当前的查询省略了您口头描述给我们的一些逻辑。试试这个:
select *
from yourTable
where (column1 = 1 or column2 = 1 or column3 = 1) or
((column1 = 1 and column2 = 1 and column3 = 1) and column4 = 10)
order by id asc
使用此逻辑,将返回仅包含column2 = 1的记录。