简体   繁体   English

选择在同一列中具有两个不同值的行

[英]Select rows that have two different values in same column

I would like to select all students that have been passed semester 1 and 4 , with my query it shows only one row by student, I was expecting two. 我想选择已通过了所有的学生semester 14,用我的查询,仅显示一个由学生行,我所期待的两项。

SELECT Student.mat_stud, fname, lname, dbirth, materials_notes, semester
FROM Student, Notes 
WHERE Notes.mat_stud = Student.mat_stud
    AND fails_status = 1
    AND Notes.mat_div = 1
    AND semester IN(1 , 4)
    AND Notes.level = 1 
    AND school_year = 2015
Group By Notes.mat_stud
Having count(fname) = 2
Order By Notes.mat_stud

My database tables looks like this: 我的数据库表如下所示:

Student
-------------------------
mat_stud    fname   lname
-------------------------
142-3698    fname   name    
142-3699    fname   name        
142-3701    fname   name    
142-3700    fname   name    



Notes
---------------------------------------------------------------
mat_stud    materials_notes     semester    level   school_year
---------------------------------------------------------------
142-3698                        1           1       2015
142-3699                        1           1       2015
142-3701                        1           1       2015
142-3698                        4           1       2015
142-3700                        1           1       2015
142-3700                        4           1       2015

Expected result: 预期结果:

mat_stud    fname   lname   materials_notes     semester
142-3698    fname   name                        1
142-3698    fname   name                        4
142-3700    fname   name                        1
142-3700    fname   name                        4

I come with this solution by joining the result of all students that have been pass semester 1 and 4: 我通过加入已经过第一学期和第四学期的所有学生的成绩来得到这个解决方案:

SELECT Notes.mat_stud FROM Notes 
    WHERE semester IN (1, 4) Group By mat_stud Having COUNT(*) = 2

With all students. 与所有学生。

SELECT t1.mat_stud, fname, lname, materials_notes FROM Student as t1, Notes 
INNER JOIN ( SELECT Notes.mat_stud FROM Notes 
WHERE semester IN (1, 4) Group By mat_stud Having COUNT(*) = 2) as t2 
ON t2.mat_stud = t1.mat_stud  
WHERE fails_status = 1 
AND Notes.mat_div = 1 
AND Notes.level = 1 
AND school_year = 2015 
AND t1.mat_stud = Notes.mat_stud 
Order By Notes.mat_stud
select S.mat_stud, S.fname, S.lname, N.materials_notes, N.semester
from Notes N, student S 
where N.mat_stud = S.mat_stud and N.mat_stud in (
    select mat_stud from Notes where semester in(1,4) 
    group by mat_stud 
    having count(mat_stud) > 1
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Select 所有行在两列中具有相同的值,同时在另一列中具有不同的值? - Select all rows that have the same value in two columns, while simultaneously having different values in another column? SQL选择两列中具有不同值的行 - SQL select rows with different values in two column 如何选择两列中具有相同值集的行,从而将第三列中的值连接起来? - How to select rows which have same set of values in two columns and hence concatenate the values in the third column? 比较 Big Query 中 A 列具有不同值且 B 列具有相同值的连续两行中的时间戳 - comparing timestamps in two consecutive rows which have different values for column A and the same value for column B in Big Query 如何选择第1列中具有相同值但第2列中具有不同值的行? - How to SELECT rows that has same values in column 1 but different values in column 2? Select 行,其中不同行中的列必须具有完全相同的值 - Select rows where column in different rows must have exactly the values 给定另一列,选择一列具有不同值的行 - Select rows that have different values for a column, given another column Mysql select 不同行不同列的相同值 - Mysql select the same values in different rows and different column SQL SELECT ID WHERE 具有相同 ID 的行具有不同的值 - SQL SELECT ID WHERE rows with the same ID have different Values 如何在Select语句中显示与两列相同的同一行的不同行? - How to show different rows of same column as two columns in Select statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM