如何编写具有多个字段的mysql查询(在where子句中具有组合)

时间:2018-06-19 11:56:35

标签: mysql

please have a look on imgae

在上图中,我有6个字段,从中我可以搜索6个字段的任意组合,它只能是classclass-sectionclass-section-caste

我如何编写一个独立于参数数量的查询。

是否可以在and子句中使用and或

class=Frist or (class=first and section=A) or (class=first and section=A and gender=Male)

是否可以这样做,或者有什么更好的方法可以实现?

修改 请看一下查询

query = currentSession.createQuery("select s.studentAdmissionId as studentAdmissionId, s.adsmissionNum as studentAdmnNum ,s.firstname as studentFirstName , s.lastname as studentLastName , s.adharNum as adharNumber ,"
                        + "  peDe.contactNum as mobileNumber  ,m.className as className ,sec.sectionName as sectionName from StudentDetails s, StudentPresentClassDetails pd, MasterCampusClass m ,"
                        + "MasterCampusSection sec , StudentParentDetails peDe  where s.studentAdmissionId = peDe.studentAdmissionId and (pd.studentAdmissionId = s.studentAdmissionId and pd.classId=m.classId) and (pd.studentAdmissionId = s.studentAdmissionId and pd.presentSectionId = sec.sectionId) and pd.classId = :classId or sec.sectionId .........");

3 个答案:

答案 0 :(得分:1)

@MạnhQuyếtNguyễn谢谢您的示例代码。

代码中有一些错误。我使用@MạnhQuyếtNguyễn示例代码

编写了代码

在这里

 Map<String, Object> params = new HashMap<>();
            if (classId != null && !classId.equals("Select")) { 
                sql.append(" AND pd.classId = :classId ");
                params.put("classId", classId); // Or values you want
            } 

            if(sectionId != null && !sectionId.equals("Select"))
            {
                 sql.append(" AND pd.presentSectionId = :presentSectionId ");
                 params.put("presentSectionId", sectionId); // Or values you want
            }


            if(gender != null && !gender.equals("Select"))
            {
                 sql.append(" AND s.gender = :gender "); 
                 params.put("gender", gender); // Or values you want
            }

            if(religion != null && !religion.equals("Select"))
            {
                 sql.append(" AND s.religionId = :religionId "); 
                 params.put("religionId", religion); // Or values you want
            }
            if(casteId != null && !casteId.equals("Select"))
            {
                 sql.append(" AND mcaste.casteId = (select mm.parentId from  MasterCaste mm where mm.casteId= :casteId ) "); 
                 params.put("casteId", Integer.parseInt(casteId)); // Or values you want
            }


            query = currentSession.createQuery(sql.toString());

            System.out.println(sql.toString());  

            for (Entry<String, Object> parameters: params.entrySet()) 
            {

                query.setParameter(parameters.getKey(), parameters.getValue());
            }

感谢每一个人!

答案 1 :(得分:0)

您可以使用多个if语句来创建查询:

StringBuilder sql = new StringBuilder("SELECT * FROM tableA WHERE 1 = 1 ");
Map<String, Object> params = new HashMap<>();
if (classValue!= null && !classValue.isEmpty()) {
    sql.append("AND class = :class ");
    params.add("class", classValue); // Or values you want
}
if (sectionValue!= null && !sectionValue.isEmpty()) {
    sql.append("AND section = :section ");
    params.add("section", sectionValue); // Or values you want
}
...
Query query = session.createQuery(sql.toString());
for (Entry<String, Object> parameters: params) {
    query.setParameter(parameters.getKey(), parameters.getValue());
}

答案 2 :(得分:0)

SELECT  *
FROM    testTable
WHERE   IF(
            :filter1 IS NOT NULL,
            col1 = :filter1,
            1 = 1
        )
        AND IF(
            :filter2 IS NOT NULL,
            `col2` = :filter2,
            1 = 1
        )
        AND IF(
            :filter3 IS NOT NULL,
            col3 = :filter3,
            1 = 1
        );

以此类推。简洁的SQL