postgres组合两个where子句

ylamdve6  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(318)

我有一个有效的问题:

"SELECT * FROM contract LEFT OUTER JOIN con_name on contract._id = con_name.contract WHERE $search_type ILIKE '%$search_word%' ORDER BY $sort_type ASC  ".

但我想添加另一个where子句,比如:

WHERE(
  $times_type >= $from_date
  AND $times_type <=  $to_date)

请问如何将这两个语句组合成一个查询?

iklwldmw

iklwldmw1#

只需将where条件与 AND 如果你想两者同时有效。

SELECT * 
FROM contract LEFT OUTER JOIN con_name on contract._id = con_name.contract
WHERE $search_type ILIKE '%$search_word%' AND $times_type >= $from_date AND $times_type <=  $to_date ORDER BY $sort_type ASC

如果一个true就足够了,可以使用括号和或

SELECT *
FROM contract LEFT OUTER JOIN con_name on contract._id = con_name.contract
WHERE $search_type ILIKE '%$search_word%' OR ($times_type >= $from_date AND $times_type <=  $to_date) ORDER BY $sort_type ASC

相关问题