简体   繁体   English

MySQL和PHP:Sum(marks)以及where子句

[英]MySQL & PHP: Sum(marks) along with where clause

I have been trying to make a query to show the rows in the table that correspond to my query and get their results but it is not working as it shows only one row. 我一直在尝试进行查询,以显示表中与我的查询相对应的行并获取其结果,但是由于它仅显示一行,因此无法正常工作。 My query is: 我的查询是:

SELECT *, sum(marks) 
FROM studentresult, subject 
where studentresult.term='1' 
  and studentresult.studentid='2' 
  and studentresult.year='2015' 
  and studentresult.subjectid = subject.id

It should be like this: 应该是这样的:

Subjectid | Studentid | Marks | Term | Year
1         | 2         | 99    | 1    | 2015
1         | 2         | 2     | 1    | 2015
                        101

But it is showing like this: 但是它显示如下:

Subjectid | Studentid | Marks | Term | Year | Sum(mark)
1         | 2         | 99    | 1    | 2015 | 101

Can someone help me? 有人能帮我吗?
Thanking you in advance 预先谢谢你

rgds rgds
Akshat 阿克沙特

SELECT subjectId, studentID, marks, term, Year, sum(marks)
FROM studentresult SR
 LEFT JOIN subject S 
where sr.term='1' 
  and sr.studentid='2' 
  and sr.year='2015' 
  and sr.subjectid = subject.id
Group by subjectId, studentID, marks, term, Year

I'm not sure what tables have what columns, so you'll need to adjust this... but you need a group by to get the multiple rows or mysql group by extension will randomly select values in the non-aggregated columns. 我不确定哪些表有哪些列,所以您需要调整它……但是您需要一个group by来获取多行,或者mysql group by extension会在非聚合列中随机选择值。 Generally you need to group by all the non-aggregated columns in the select provided they would have different values. 通常,您需要按选择中所有未汇总的列进行分组,前提是它们的值不同。 If they have the same value, then mysql group by extension can handle it and they are not needed in the group by. 如果它们具有相同的值,则mysql group by extension可以处理它,并且group by中不需要它们。

Logically in this case, I think all values in your expected result should be in the group by as you may have the same person, over multiple years and terms. 从逻辑上讲,在这种情况下,我认为您的预期结果中的所有值都应归为一组,因为您可能具有相同的人,并且经历了多年和期限。 Mark is the only value which May be able to be excluded. 标记是唯一可以排除的值。

Now that I see you want a sum of marks though... we'll have to do a sub-select to get that value as mySQL doesn't have the windowed sets logic. 现在,我看到您想要一个标记的总和...我们将不得不进行子选择以获取该值,因为mySQL没有窗口化集合逻辑。 using over syntax... This windowed set logic allows you do do inline aggregation without effecting the results of the main query. 正在使用over语法...此窗口化设置逻辑使您可以进行内联聚合,而不会影响主查询的结果。 Since mySQL doesn't support it, we use a subselect. 由于mySQL不支持它,因此我们使用子选择。

While I switched to using the INNER JOIN syntax vs the , notation, both would work; 当我改用INNER JOIN语法而不是,符号时,两者都可以工作。 I'm just more comfortable with the Inner join syntax. 我对内部连接语法更满意。

SELECT subjectId, studentID, marks, term, Year, sMarks
FROM studentresult SR
INNER JOIN subject S 
  on  SR.subjectid = s.id
INNER JOIN (SELECT sum(Marks) smarks, Year, Term, StudentID, Subject
            FROM studentResult
            GROUP BY Year, Term, StudentID, Subject) Sub
   ON SR.studentID =Sub.StudentID
  and SR.Subject = Sub.Subject
  and SR.Year = Sub.Year
  and SR.Term = Sub.Term
WHERE SR.term='1' 
  and SR.studentid='2' 
  and SR.year='2015' 

I know you already accepted an answer but just to let you know that, in fact, you can achieve exactly what you want. 我知道您已经接受了答案,但只是让您知道,实际上,您可以完全实现所需的目标。 Also, since you don't retrieve any data from subject table, you can remove the join to it. 另外,由于您没有从主题表中检索任何数据,因此可以删除对其的联接。

SQL Fiddle SQL小提琴

MySQL 5.5.32 Schema Setup : MySQL 5.5.32模式设置

CREATE TABLE studentresult
    (`Subjectid` int, `Studentid` int, `Marks` int, `Term` int, `Year` int)
;

INSERT INTO studentresult
    (`Subjectid`, `Studentid`, `Marks`, `Term`, `Year`)
VALUES
    (1, 2, 99, 1, 2015),
    (1, 2, 2, 1, 2015)
;

CREATE TABLE subject
    (`id` int, `Subject` varchar(8))
;

INSERT INTO subject
    (`id`, `Subject`)
VALUES
    (1, 'Whatever')
;

Query 1 : 查询1

SELECT subjectId, studentID, marks, term, Year
FROM studentresult
INNER JOIN subject ON studentresult.subjectid = subject.id
WHERE term='1' 
  and studentid='2' 
  and year='2015' 
UNION
SELECT '', '', sum(marks), '', ''
FROM studentresult 
INNER JOIN subject ON studentresult.subjectid = subject.id
WHERE term='1' 
  and studentid='2' 
  and year='2015' 
GROUP BY subjectId, studentID, term, Year
ORDER BY marks

Results : 结果

| SUBJECTID | STUDENTID | MARKS | TERM | YEAR |
|-----------|-----------|-------|------|------|
|         1 |         2 |     2 |    1 | 2015 |
|         1 |         2 |    99 |    1 | 2015 |
|           |           |   101 |      |      |

I have to tell you sir that you cannot append a custom row after your query. 先生,我必须告诉您,您无法在查询后附加自定义行。 The tables (query results) are joined. 表(查询结果)已联接。 The result is showing correctly. 结果正确显示。 you need to divide the query into 2 sub queries: 您需要将查询分为2个子查询:

SELECT *
FROM studentresult, subject 
where studentresult.term='1' 
  and studentresult.studentid='2' 
  and studentresult.year='2015' 
  and studentresult.subjectid = subject.id

And to get the sum u can have: 为了获得总和,您可以拥有:

SELECT sum(marks) 
FROM studentresult, subject 
where studentresult.term='1' 
  and studentresult.studentid='2' 
  and studentresult.year='2015' 
  and studentresult.subjectid = subject.id

It is the better way to do it. 这是更好的方法。 Just so you know it is not always efficient to join your queries... 请注意,加入查询并不总是那么有效...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM