具有多个where语句的多个选择查询

时间:2016-02-22 23:11:44

标签: php mysql

我目前有这个查询:

SELECT s.firstName, s.lastName, sum(correct) AS score 
FROM student s 
JOIN test t ON s.id = t.studentid 
WHERE t.testType = 1 GROUP BY lastName, firstName

但我也想要它显示

WHERE t.testType = 2

和testType 3和4也是。

我想把它全部放在同一行。这可能吗?

3 个答案:

答案 0 :(得分:0)

您可以使用in样式从多个不同值中进行选择

SELECT s.firstName, s.lastName, sum(correct) AS score 
FROM student s 
JOIN test t ON s.id = t.studentid 
WHERE t.testType in ( 1,2,3,4 ) 
GROUP BY lastName, firstName

答案 1 :(得分:0)

我认为在条款中可以回答你的问题 - http://www.tutorialspoint.com/mysql/mysql-between-clause.htm

where t.testType between 1 and 4

答案 2 :(得分:0)

SELECT s.firstName, s.lastName, b.score, c.score2
FROM student s 
JOIN (
    SELECT s.id,s.firstName, s.lastName, sum(correct) AS score FROM student s JOIN test t ON s.id = t.studentid WHERE t.testType = 1 GROUP BY lastName, firstName
) b ON (s.id = b.id)
JOIN (
    SELECT s.id,s.firstName, s.lastName, sum(correct) AS score2 FROM student s JOIN test t ON s.id = t.studentid WHERE t.testType = 2 GROUP BY lastName, firstName
) c ON (s.id = c.id)
GROUP BY lastName, firstName

我刚刚使用了一堆JOIN语句。有用。如果任何一个值为0,则不显示任何内容。