比如A表有id,name,age三個欄位
當id欄位查詢條件為空時,sql陳述句為where name='x' and age='y';
當name欄位查詢條件為空時,sql陳述句為where id='x' and age='y‘;
當age欄位輸入的查詢條件為空時,sql陳述句為where id='x' and name='y';
求助sql怎么寫’
uj5u.com熱心網友回復:
各位大神大神。uj5u.com熱心網友回復:

uj5u.com熱心網友回復:
動態拼接SQL。樓主可以借鑒一下 mybatis 。
uj5u.com熱心網友回復:
select id,age,name from tab where id is null and age = 'x' and name = 'y'union all
select id,age,name from tab where name is null and id = 'x' and name = 'y'
union all
select id,age,name from tab where age is null and id = 'x' and name = 'y'
uj5u.com熱心網友回復:
瞎搞
uj5u.com熱心網友回復:
用mybatis這些都有自己判斷的uj5u.com熱心網友回復:
SELECT *FROM a
WHERE (id IS NULL AND NAME = 'x' AND age = 'y')
OR (NAME IS NULL AND id = 'x' AND age = 'y')
OR (age IS NULL AND id = 'x' AND NAME = 'y')
uj5u.com熱心網友回復:
這個不行,如果某個欄位為空那么查詢條件里就不要這個欄位,而不是用null來篩選uj5u.com熱心網友回復:
版主詳細一點啊- - 。uj5u.com熱心網友回復:
select * from a
where decode(id,null,name,'x')='x' and decode(id,null,age,'y')='y'
and decode(name,null,id,'x')='x' and decode(name,null,age,'y')='y'
and decode(age,null,id,'x')='x' and decode(age,null,name,'y')='y';
uj5u.com熱心網友回復:
使用or 或unionuj5u.com熱心網友回復:
<!-- ElementID:selectCycleByRepId,Generated=false --><select id="selectCycleByRepId" parameterType="map" resultMap="T_STAT_CONFIG_ASSESSMENTResult">
select
<include refid="column_sql"/>
from T_STAT_CONFIG_ASSESSMENT where 1=1
<if test="conassid != null">and T_STAT_CONFIG_ASSESSMENT.conassid = #{conassid}</if>
<if test="repId != null">and T_STAT_CONFIG_ASSESSMENT.rep_id = #{repId}</if>
<if test="cycleType != null">and T_STAT_CONFIG_ASSESSMENT.cycle_type = #{cycleType}</if>
</select>
可以用這種mybatis的方式
uj5u.com熱心網友回復:
decode和when case可以解決uj5u.com熱心網友回復:
10樓已經寫出來了,可以看下邏輯轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/74965.html
標籤:開發
下一篇:商品圖片顯示 SQL陳述句
