Firebird, c#中的查询,不同值的不同查询,如何构造和简化

本文关键字:查询 何构造 Firebird | 更新日期: 2023-09-27 18:10:11

选择月份,年份和以下代码(伪代码):

if(month.checked) query = select* from tab where month=month.text;
if(year.checked) query = select* from tab where year=year.text;
if(month.checked and year.checked) query = select * from tab where month=month.value and year=year.value
if(!month.checked and !year.checked) query = select* from tab

所以你看到有4个不同的查询。有可能在一个查询中做得更快吗?

Firebird, c#中的查询,不同值的不同查询,如何构造和简化

从技术上讲,您可以对此进行一次查询,但查询会变得更加复杂,这通常会导致查询速度变慢。除非你有一个令人信服的理由,否则就坚持单独的查询。

类似下面的查询可以达到这个目的:

SELECT *
FROM tab
WHERE ? IS NOT NULL AND month = ?
OR ? IS NOT NULL AND year = ?
OR ? IS NULL AND ? IS NULL

这里参数1和5是如果month被选中的指示(NULL表示未选中,一个值表示选中),参数2是month的值。

参数3和参数6为year检查时的指示符,参数4为year. Text的值。