怎么写这样的SQL语句?很难!!

dddeee 2004-11-20 07:32:52

要写一个函数,传入一些参数。把这些参数作为查询条件,但是这些参数可能有值,但同时也可能不需要。没说清楚吧,看这个例子。

一个函数
public foo(String key1, String key2, String key3) {
....
//写查询语句,以它们三个为条件
//如果这三个条件都有值,很简单
sqlStr = "select * from table where key1=" + key1 + " and key2=" + key2 + " and key3=" + key3 + ";";

//但是这三个key都可能为null,我们有时只需要查询一个或两个key,忽略掉另一个。
 //再写sql语句就麻烦了。
sqlStr = "select * from table ";
if(key1==null) {
if(key2==null) {
if(key3==null) {
sqlStr += ";";
} else {
sqlStr += "where key3=" + key3 + ";";
}
} else {
sqlStr += " where key2=" + key2;
if(key3==null) {
sqlStr += ";"
} else {
sqlStr += " and key3=" + key3 + ";";
}
}
} else {
sqlStr += " where key1=" + key1;
if(key2==null) {
if(key3==null) {
sqlStr += ";";
} else {
sqlStr += " and key3=" + key3 + ";";
}
} else {
sqlStr += " and key2=" + key2;
if(key3==null) {
sqlStr += ";";
} else {
sqlStr += " and key3=" + key3 + ";";
}
}
}
}

要是参数再多一些,如5个,10个,怎么办?有没有简单一些的写法?
...全文
112 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
double22822 2004-11-27
  • 打赏
  • 举报
回复
to dddeee(dddeee) :
你有没有看我的最后一个判断,我是在所有的前面都加了 and 然后判断 sqlstr 是不是为空,如果不为空,去掉前面五个字符,就是把 and 去掉,最后同where 组成sql语句,如果sql是空的话,那就没有where语句.
mathematician 2004-11-27
  • 打赏
  • 举报
回复
这个问题在编程中经常遇到,解决方法有很多。前面 chaozi(编程浪子)的方法是我平时最常用的,在MYSQL和SQL SERVER中都不会产生语法错误,不知道楼主用的是什么数据库?trampwind(随风)的方法也有异曲同工之处。 double22822(大无忧-老实和尚(有事发消息)) 的方法可行,不过这么写程序,是不是太罗嗦? :)
trampwind 2004-11-24
  • 打赏
  • 举报
回复
很容易嘛,先写语句头:sqlStr = "select * from table where true ",
后面再根据判断每个参数是否为空来决定该参数是否附加到条件语句中,
if(key1 != null){
sqlStr += " and key1=" + key1;
}
if(key2 != null){
sqlStr += " and key2=" + key2;
}
if(key3 != null){
sqlStr += " and key3=" + key3;
}
.....
patty79 2004-11-24
  • 打赏
  • 举报
回复
if(key1 != null){
sqlStr += " and key1=" + key1;
}
if(key2 != null){
sqlStr += " and key2=" + key2;
}
if(key3 != null){
sqlStr += " and key3=" + key3;
}
.....

这个不错
abent 2004-11-24
  • 打赏
  • 举报
回复
和尚是对的,而且在代码后面附了详细说明了,楼主没看清楚,很简单的字符串处理啊:
public foo(String key1, String key2, String key3) {
....
//写查询语句,以它们三个为条件
//如果这三个条件都有值,很简单
//sqlStr = "select * from table where key1=" + key1 + " and key2=" + key2 + " and key3=" + key3 + ";";

sqlStr = "select * from table ";
String sqlWhere = "";
if(key1 != null){
sqlWhere += " and key1=" + key1;
}
if(key2 != null){
sqlWhere += " and key2=" + key2;
}
if(key3 != null){
sqlWhere += " and key3=" + key3;
}
if(key4 != null){
sqlWhere += " and key4=" + key4;
}
...

if(sqlWhere == ""){ //如果没有字符则说明所有条件都是null值,直接加";"
sqlWhere = ";";
}
else{ //否则就肯定至少有一个条件非null,则将结果字符串前面的" and "去掉
sqlWhere = "where" + sqlWhere.replace(/^and /g,"") + ";";
}
//拼装最后的结果
sqlStr +=sqlWhere;
...
}
dddeee 2004-11-22
  • 打赏
  • 举报
回复
to chaozi(编程浪子)

如果key1==null的话
sqlStr 不就是 select * from table where 2>1 and key1="" and key2=。。。。

显然语法错误

to double22822(大无忧-老实和尚(有事发消息)) :
你的写法不是
select * from table where and key1=......
也是语法错误

关键在于
where后面的第一个条件没有and,而其它的条件后面有and
但是哪一个才是第一个条件,判断起来很麻烦
double22822 2004-11-22
  • 打赏
  • 举报
回复
sqlselct = "select * from table ";
sqlStr = "";
if(key1!=null) {
sqlStr += " and key1=" + key1;
}

if(key2!=null) {
sqlStr += " and key2=" + key2;
}
....
if len(sqlStr) > 0 {
sqlStr = mid(sqlStr,5);
sqlselct += " where " + sqlStr;
}

我不是学mysql的,大体就这个意思.len是求字符串长度的意思,mid()是从第几位去字符串到最后.
寒舍人 2004-11-22
  • 打赏
  • 举报
回复
你可以写到一起啊,在where后跟一个真值,如2>1,
sqlStr = "select * from table where 2>1 and key1=" + key1 + " and key2=" + key2 + " and key3=" + key3 + ";";
这样可以吧。如果害怕 key1 为null,你最好加一个判断。
if(key1==null){
key1="";
}
dddeee 2004-11-21
  • 打赏
  • 举报
回复
UP?
dddeee 2004-11-20
  • 打赏
  • 举报
回复
但还是不够简单。
而且没有办法使用prepareStatement,只能用executeQuery(sqlStr)来查询
dddeee 2004-11-20
  • 打赏
  • 举报
回复
或者这样:
sqlStr = "select * from table ";
if(key1!=null) {
if(sqlStr.indexOf("where")>0) {
sqlStr += " and key1=" + key1;
} else {
sqlStr += " where key1=" + key1;
}
}
if(key2!=null) {
if(sqlStr.indexOf("where")>0) {
sqlStr += " and key2=" + key2;
} else {
sqlStr += " where key2=" + key2;
}
}
if(key3!=null) {
if(sqlStr.indexOf("where")>0) {
sqlStr += " and key3=" + key3;
} else {
sqlStr += " where key3=" + key3;
}
}
sqlStr += ";";

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧