当前位置: 移动技术网 > IT编程>开发语言>Java > SQL高级查询

SQL高级查询

2019年05月31日  | 移动技术网IT编程  | 我要评论

本文主要是对下图的查询方式的条件判断进行讲解:

如果还有更多的查询条件,只要在下面方法中加入相对应的字段,然后添加相应的get和set方法,最后在

getquerycondition方法中加入相对应的条件判断语句if就可以了。
public class sqlcondition {
    
    //职位:用于查询的条件判断
    private string  title;
    //职位类型:用于查询的条件判断
    private integer positiontype;
    
    public sqlcondition() {
        super();
    }
    
    public string gettitle() {
        return title;
    }

    public void settitle(string title) {
        this.title = title;
    }

    public integer getpositiontype() {
        return positiontype;
    }

    public void setpositiontype(integer positiontype) {
        this.positiontype = positiontype;
    }
    
    /**
     * 查询语句的条件判断
     * 方法一(推荐使用):
     */
    public string getquerycondition_01(){
        string wheresql="";
        if(title !=null && !"".equals(title)){
            wheresql += " and title like '%"+title+"%'";
        }
        if(positiontype!=null && !"".equals(positiontype)){
            wheresql+=" and positiontype = "+positiontype;
        }
        //replace:替换;first:第一个
        return wheresql.replacefirst("and", "where");
    }
        
     /**
     * 查询语句的条件判断
     * 方法二(不推荐使用): where 1==1 会影响查询的性能
     */
    public string getquerycondition_02(){
        string  wheresql = "where 1==1";

        if(title != null && !"".equals(title)){
            wheresql+= "and title like '%"+title+"%'";
        }
        if(positiontype !=null){
            wheresql += "and positiontype = " +positiontype;
        }

        return wheresql;
    }

    /**
     * 查询语句的条件判断
     * 方法三(准备一个标识(即一个flag)
         如果标识是true,条件前就加where,如果是false,条件前就加and):
     */
    public string getquerycondition_03(){
        //标识:flag
        boolean flag = true;
        string wheresql = "";
        //title不为null,并且不为空字符串
        if(title!=null && !"".equals(title)){
            if(flag){
                wheresql+= " where ";
                flag = false;
            }else{
                wheresql+= " and ";
            }
            wheresql += " title like '%"+title+"%' ";
        }
        if(positiontype!=null){
            if(flag){
                wheresql+= " where ";
                flag = false;
            }else{
                wheresql+= " and ";
            }
            wheresql += " positiontype = "+positiontype;
        }
        return wheresql;
    }

    /**
     * 查询语句的条件判断
     * 方法四(准备一个集合):
     */
    public string getquerycondition_04(){
        //准备一个集合(里面会装咱们的所有条件)
        list<string> sqllist = new arraylist<>();

        string wheresql = "";
        //title不为null,并且不为空字符串
        if(title!=null && !"".equals(title)){
            sqllist.add(" title like '%"+title+"%' ");
        }
        if(positiontype!=null){
            sqllist.add(" positiontype = "+positiontype);
        }
        //查询条件加多了,只要在这加if语句就可以了
        //遍历这个集合
        for (int i = 0; i < sqllist.size(); i++) {
            if(i==0){
                //第一次循环,前面肯定是加where
                wheresql += " where ";
            }else{
                //第2-n次循环,前面肯定是加and
                wheresql += " and ";
            }
            wheresql += sqllist.get(i);
        }

        return wheresql;
    }

最后在需要的sql语句中引入条件判断方法即可。

 

四种方法特点分析:

  方法一:比较简单,容易看懂,不会影响查询性能,推荐使用。

  方法二:虽然比方法一少了几个代码,但 where == 1在sql查询中会影响查询性能,不建议使用。

  方法三:代码比较多,容易写错。

  方法四:比较难理解,使用相对其它几个方法比较麻烦。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网