需求:查詢進行中的活動資料
進行中一共有以下幾種情況:
1.開始時間為空,結束時間為空, 此結果資料将永遠為進行中的資料
2.開始時間為空,結束時間不為空,則目前時間在結束時間之前,為進行中的資料
3.開始時間不為空,結束時間為空,則目前時間在開始時間之後,為進行中的資料
4.開始時間不為空,結束時間不為空,則目前時間在開始與結束時間段之内的資料為進行中資料
下面sql則查詢的是滿足以上四種需求的結果集,達标題需求
SELECT * FROM
表名
WHERE 1=1
and(start_time is null or start_time<now())
and(end_time is null or end_time>now())
mybatis寫法,開始時間與結束時間傳入參數允許為空
如圖所示:
<if test="record.startDate != null and record.startDate != '' or record.endDate != null and record.endDate != '' ">
AND id in
(select id from rht_product_price where 1=1
<if test="record.startDate != null and record.startDate != ''">
and start_date <= #{record.startDate,jdbcType=VARCHAR}
</if>
<if test="record.endDate!= null and record.endDate != ''">
and end_date >= #{record.endDate,jdbcType=VARCHAR}
</if>
)
</if>