通过多个时间比较获得单个列值的结果

时间:2017-01-07 05:12:08

标签: sql

MARKS

STUDENT COL_SKILL   COL_MARKS
-----------------------------
1       PHP         5
1       SQL         3
1       HTML        2
1       CSS         2
2       PHP         1
2       SQL         1
3       PHP         3
3       HTML        2
3       CSS         2

我想得到

的结果
SELECT STUDENT 
FROM MARKS 
WHERE SKILL = 'PHP' AND MARKS >= 3

SELECT STUDENT 
FROM MARKS 
WHERE SKILL = 'HTML' AND MARKS >= 2

我想在一个查询中获取学生列表。

3 个答案:

答案 0 :(得分:1)

您可以使用UNION ALL之类的:

SELECT STUDENT 
FROM MARKS 
WHERE SKILL='PHP' AND MARKS >=3
  UNION ALL
SELECT STUDENT 
FROM MARKS 
WHERE SKILL='HTML' AND MARKS >=2

如果您想在学生列中使用不同的值,请使用UNION,如:

SELECT STUDENT 
FROM MARKS 
WHERE SKILL='PHP' AND MARKS >=3
  UNION
SELECT STUDENT 
FROM MARKS 
WHERE SKILL='HTML' AND MARKS >=2

答案 1 :(得分:1)

使用OR条件

SELECT STUDENT FROM MARKS 
WHERE (SKILL='PHP' AND MARKS >=3) 
   OR (SKILL='HTML' AND MARKS >=2)

答案 2 :(得分:1)

相当于您要尝试的是使用OR

SELECT STUDENT FROM MARKS 
WHERE (SKILL='PHP' AND MARKS >=3) 
     OR (SKILL='HTML' AND MARKS >=2)

如果您想在HTML中找到3个或更多PHP 2个或更多标记的学生,请尝试以下方法:

select student
from marks
group by student
having count(skill = 'PHP' and marks >= 3) > 0
and count(skill = 'HTML' and marks >= 2) > 0;