介绍:我要实现一个查询
表student
假如字段如下
字段: id name age sex
查询条件可能只有一个,也可能两个,..
select * from strudent where id .., name .., age.., sex..
由于查询条件不确定的,在程序里面传,可能为空,如果为空就必须查所有的
例:我要查age为‘18’性别为'm'的所有记录那么name与id为空我在sql
只传给age和sex ,另外两个同样也要赋值,能不能给我写一个方法
我自己想的方法:
select * from student where name like ? , id like ? , age like ? ,sex like ?程序:每个问号都是通过程序来赋值的
由于name 和 id都为空所以应该传‘%?%’
其他两个就赋上时间的值
我sql就会点皮毛,有没有高手给解决,多谢!
表student
假如字段如下
字段: id name age sex
查询条件可能只有一个,也可能两个,..
select * from strudent where id .., name .., age.., sex..
由于查询条件不确定的,在程序里面传,可能为空,如果为空就必须查所有的
例:我要查age为‘18’性别为'm'的所有记录那么name与id为空我在sql
只传给age和sex ,另外两个同样也要赋值,能不能给我写一个方法
我自己想的方法:
select * from student where name like ? , id like ? , age like ? ,sex like ?程序:每个问号都是通过程序来赋值的
由于name 和 id都为空所以应该传‘%?%’
其他两个就赋上时间的值
我sql就会点皮毛,有没有高手给解决,多谢!
name的?
-------------------
if ?="" then
trim(name) is null
else
name=?
endifid的?
-------------------
if ?="" then
trim(id) is null
else
id=?
endif最后形成的样式:
select * from student
where trim(name) is null and id=?
and age like ? and sex like ?