NutzCN Logo
问答 自定义SQL跟Cnd如何整合
发布于 1790天前 作者 SwainHo 1656 次浏览 复制 上一个帖子 下一个帖子
标签:

比如想查询这个sql,当参数不为空的时候加上条件

select a.number,a.time,a.status," +
                "b.course_id,b.quantnum,b.price,b.orig," +
                "c.name from order a " +
                "left join course_order b on a.id = b.order_id " +
                "left join course c on b.course_id = c. id" +
                "where a.user_id=@userId

应该用哪种方法把sql和cnd结合起来

3 回复
Sql sql = Sqls.create("select a.number,a.time,a.status," +
                "b.course_id,b.quantnum,b.price,b.orig," +
                "c.name from order a " +
                "left join course_order b on a.id = b.order_id " +
                "left join course c on b.course_id = c. id" +
                " $condition");
sql.setCondition(Cnd.NEW().andEX("a.user_id", "=", userId));

cnd.andEX/orEX 均有类似效果

@wendal

Sql sql = Sqls.create("select a.number,a.time,a.status," +
                "b.course_id,b.quantnum,b.price,b.orig," +
                "c.name from order a " +
                "left join course_order b on a.id = b.order_id " +
                "left join course c on b.course_id = c. id" +“where 1=1+ $condition”
                " ");
sql.setCondition(Cnd.NEW().andEX("a.user_id", "=", userId));

如果参数可能为空的话要加一个where 1=1防止报错么,如果加的话应该加到sql里还是setCondition里

你可以试试哦...

添加回复
请先登陆
回到顶部