求最佳的代码
牧牛人软件 2003-09-13 07:36:49 Sql="select * from tab where amount=:Variable0 and date between :Variable1 and :Variable2 and (account not like :Variable3 or code > :Variable4)";
我想用型如上列的SQL作查询,查询的界面如下:
Amount____________ Date1__________ Date2__________
Account___________ Code______
当用户输入Amount、Date、Account、Code的值后,我把这些常量分别填入:Variable[n]中,然后访问数据库。
【问题】如果用户在某个FIELD没有输入(既为空)就在WHERE中取消查询条件。
例如:
1。Code为空 Where子句就改成:where amount=:Variable0 and date between :Variable1 and :Variable2 and (account not like :Variable3)";
2。Amount为空 Where子句就改成where date between :Variable1 and :Variable2 and (account not like :Variable3 or code > :Variable4)";
3。Date1&Date2为空 Where子句就改成where amount=:Variable0 and (account not like :Variable3 or code > :Variable4)";
4。Account为空 Where子句就改成where amount=:Variable0 and date between :Variable1 and :Variable2 and (code > :Variable4)";
.................
如此等等。
【要求】要考虑各种情况,取消前后相关的关系符,写出最佳的代码。