Home > Database > Mysql Tutorial > body text

MySQL中对查询的结果集取交集如何实现_MySQL

WBOY
Release: 2016-06-01 13:38:42
Original
1348 people have browsed it

bitsCN.com


MySQL中对查询的结果集取交集如何实现

 

需求:通过入学批次、层次、课程名称、专业和统考科目 为教学课程与统考科目设定对应关系,可通过选多个专业对教学课程与统考课程进行对应。

     

问题:级联---入学批次影响层次,入学批次与层次影响专业、入学批次层次与专业影响课程名称。当选了多个专业时,课程名称下拉框如何显示多个专业共有的课程(如果不能保证课程共有,可能会添加一些专业没有的课程对应关系)呢。

 

解决方案:

为了从略,这里只写出入学批次层次与专业影响课程名称的级联,也就是通过入学批次层次与专业来查询课程名称,传值都用ID,查询参数设置省略了。

 

方案一、临时表:

String queryBasic= “select model from ” ;

StringBuilder queryString=new StringBuilder();

queryString.append(queryBasic);

For(int i=0;i

       queryString.append(“(select model from TeachingPlan_CourseDetail model where model.grade=:grade and model.educationLevel and model.specialId=:specialId)  a”);

       queryString.append(i+ “”);

       if(i

              queryString.append(“,”);

}

}

If(specialIds.length>1){

queryString.append(“ where a”);

queryString.append(1+ “”);

queryString.append(“. specialId”

if(i

                     queryString.append(“=”);

}

}

 

方案二、交集join

String queryBasic= “select model from ”;

StringBuilder queryString=new StringBuilder();

queryString.append(queryBasic);

For(int i=0;i

       queryString.append(“(select model from TeachingPlan_CourseDetail model where model.grade=:grade and model.educationLevel and model.specialId=:specialId) as a ”);

       queryString.append(i+ “”);

       if(i

              queryString.append(“cross”);

}

}

If(specialIds.length>1){

For(int i=0;i

              queryString.append(“a”+i);

              queryString.append(“.specialId”);

              if(i

                     queryString.append(“=”);

}

}

}

方案三、子查询与上面类似,略。

 

bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template