简体   繁体   English

检索列总和大于其他表中其他列的行

[英]Retrieve row where sum of column is greater than other column of different table

I have two database tables. 我有两个数据库表。

First table: 第一表:

| ID  |  Sub-Name |  Marks
| 01  |  french   |  50
| 01  |  russian  |  50
| 02  |  french   |  30
| 02  |  russian  |  50
| 03  |  french   |  20
| 03  |  russian  |  30

Second table: 第二张表:

|  ID | Stu-name | passing_marks
|  01 | abc      | 90
|  02 | xyz      | 90
|  03 | mno      | 90

I want to get the names of students whose collective marks of French and Russian are greater the passing_marks of table2. 我想获得学生的姓名,这些学生的法语和俄语集体标记大于table2的通过passing_marks

Please , First declare primarykey and foreignkey relation with table with proper table name 请首先声明primarykeyforeignkey与台关系与适当的table name

Here I take 1st named detail and second table name is student 在这里,我采用第一个命名的detail ,第二个表的名称为student

SELECT student.ID, student.Stu_name, detail.collective
FROM student
JOIN  
(
   SELECT ID, SUM(Marks) AS collective
   FROM detail GROUP BY ID
) detail
ON student.ID = detail.ID
AND detail.collective> student.passing_marks;

Try to join with sub-query like this: 尝试加入子查询,如下所示:

SELECT t2.ID, t2.Stu_name, t1.Total 
FROM Table2 t2
JOIN  
(
   SELECT ID, SUM(Marks) AS Total
   FROM Table1 GROUP BY ID
) t1
ON t2.ID = t1.ID
AND t1.Total > t2.passing_marks;

Output: 输出:

| ID | STU_NAME | TOTAL |
-------------------------
|  1 |      abc |   100 |

See this SQLFiddle 看到这个SQLFiddle

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

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