動態 SQL 匹配查詢 AND OR

2021-08-22 04:33:57 字數 1654 閱讀 4318

在接收前端傳入乙個關鍵字用來匹配資料庫的兩個欄位中的其中乙個的時候,mybatis 可以採用 and or 關鍵字進行查詢 。

and ( method like concat('%', #, '%')

or eventcode like concat('%', #, '%')

) and statusofread = #

and bizcaseid = #

傳入字段

string statusofread = "0";       string method = "u";  

string bizcaseid = "10000000012028"; string key = "pa";

執行  sql

select * from tnotification where 

( method like concat('%', ?, '%') or

eventcode like concat('%', ?, '%')

) and statusofread = ? and bizcaseid = ?

parameters: pa(string), pa(string), 0(string), 10000000012028(string)

同時也可以進行多個 or 的連線,如

select * from tnotific

and ( method like concat('%', #, '%')

or eventcode like concat('%', #, '%')

or eventname like concat('%', #, '%')

) and statusofread = #

and bizcaseid = #

執行的 sql 語句是

==>  preparing: 

select * from tnotific where ( method like concat('%', ?, '%') or eventcode like concat('%', ?, '%') or eventname like concat('%', ?, '%') ) and statusofread = ? and bizcaseid = ?

==> parameters: pa(string), pa(string), pa(string), 0(string), 10000000012028(string)

select

p.id,

m.company

from operation_merchant_product p

inner join operation_merchant m on m.id = p.merchant_id

and m.merchant_name like concat('%',#,'%')

or m.merchant_code like concat('%',#,'%')

and p.status = 0

group by p.id

order by p.create_date desc

JPA 動態查詢之AND OR結合使用

現在,我負責開發的專案中,使用jpa作為orm框架。有了jpa,一行sql都沒寫過。在昨天,有乙個新的需求,需要進行動態查詢,這個簡單。但是有乙個地方需要and or結合使用,這裡,我將記錄下我的理解與寫法,希望能幫助到大家。需要根據條件進行動態查詢,實現一條類似下文的語句 select from ...

sql 正則匹配查詢

替換非數字文字 moblidesearch preg replace 0 9 search stingsearch for number search string 乙個陣列,再用 digit implode 分開 stingsearch implode digit str split moblid...

SQL語句匹配查詢

僅查詢純數字 匹配查詢 僅sql有效 select cast spxx.id as int from spxx,lb where spxx.lbid lb.id and patindex 0 9 spxx.id 0 and len ltrim rtrim spxx.id 10 access sql都...