07-08
26
多选判断查询
作者:Java伴侣 日期:2007-08-26
竟然弄了好几个小时,查错。。。服了
先把错误的列出来:
显示如下:
因为错误的判断,造成了lable走了两次g_id,怎么解决?颠倒一下上下位置啦...
先把错误的列出来:
复制内容到剪贴板 程序代码
public String countChoose(String name, String cid, String state) { //选择算法
int lable = 0; //跟踪标记
String sqladd = ""; //动态生成的sql部分语句
String sql = "";
if (!name.equals("")) {
sqladd = "p.name='" + name + "'";//这里也需要注意,单引号
lable = 1;
}
/**
* 另外,不要用p.g_id因为有联级
*/
if (!cid.equals("") && lable == 0) {
sqladd = "p.g_id=" + Integer.parseInt(cid) + "";
log.info("1:" + sqladd);
lable = 1;
}
if (!cid.equals("") && lable == 1) {
sqladd = sqladd + " and p.g_id=" + Integer.parseInt(cid) + "";
log.info("2:" + sqladd);
}
if (!state.equals("") && lable == 1) {
sqladd = sqladd + " and p.state=" + Integer.parseInt(state) + "";
}
if (!state.equals("") && lable == 0) {
sqladd = "p.state=" + Integer.parseInt(state) + "";
lable = 1;
}
if (lable == 0) {
sql = "from ProductVO as p order by p.id desc";
} else {
sql = "from ProductVO as p where " + sqladd + " order by p.id desc";
}
log.info("search sql:" + sql);
return sql;
}
int lable = 0; //跟踪标记
String sqladd = ""; //动态生成的sql部分语句
String sql = "";
if (!name.equals("")) {
sqladd = "p.name='" + name + "'";//这里也需要注意,单引号
lable = 1;
}
/**
* 另外,不要用p.g_id因为有联级
*/
if (!cid.equals("") && lable == 0) {
sqladd = "p.g_id=" + Integer.parseInt(cid) + "";
log.info("1:" + sqladd);
lable = 1;
}
if (!cid.equals("") && lable == 1) {
sqladd = sqladd + " and p.g_id=" + Integer.parseInt(cid) + "";
log.info("2:" + sqladd);
}
if (!state.equals("") && lable == 1) {
sqladd = sqladd + " and p.state=" + Integer.parseInt(state) + "";
}
if (!state.equals("") && lable == 0) {
sqladd = "p.state=" + Integer.parseInt(state) + "";
lable = 1;
}
if (lable == 0) {
sql = "from ProductVO as p order by p.id desc";
} else {
sql = "from ProductVO as p where " + sqladd + " order by p.id desc";
}
log.info("search sql:" + sql);
return sql;
}
显示如下:
引用内容
INFO - 1:p.g_id=1
INFO - 2:p.g_id=1 and p.g_id=1
INFO - search sql:from ProductVO as p where p.g_id=1 and p.g_id=1 and p.state=0 order by p.id desc
INFO - 2:p.g_id=1 and p.g_id=1
INFO - search sql:from ProductVO as p where p.g_id=1 and p.g_id=1 and p.state=0 order by p.id desc
因为错误的判断,造成了lable走了两次g_id,怎么解决?颠倒一下上下位置啦...
评论: 0 | 引用: 0 | 查看次数: 780
发表评论