-1

我有两个数据库表。

第一张表:

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

第二张表:

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

我想得到表2中法语和俄语集体标记更大的学生的名字passing_marks

4

2 回答 2

3

请先声明primarykeyforeignkey与适当的表的关系table name

在这里,我取第一个命名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;
于 2013-07-24T05:15:50.283 回答
0

尝试像这样加入子查询:

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;

输出:

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

看到这个 SQLFiddle

于 2013-07-24T05:05:52.613 回答