执行查询时:
SELECT DISTINCT (u.name),u.class,
(SELECT ( markscored) FROM marklist WHERE subject LIKE 'biology' AND u.id=m.id) AS biology,
(SELECT ( markscored) FROM marklist WHERE subject LIKE 'chemistry' AND u.id=m.id) AS chemistry,
(SELECT ( markscored) FROM marklist WHERE subject LIKE ' physics' AND u.id=m.id) AS physics,
(SELECT SUM( markscored) FROM marklist WHERE u.id=m.id) AS TOTAL,
(SELECT SUM(outof) FROM marklist WHERE u.id=m.id) AS OUTOF,
((SELECT SUM( markscored) FROM marklist WHERE u.id=m.id)/(SELECT SUM(outof) FROM marklist WHERE u.id=m.id)*100) as percentage
FROM u, marklist m
WHERE u.id=m.userid
我收到的错误是:
错误1242(21000):子查询返回超过1行
我该如何解决这个问题?
答案 0 :(得分:0)
以下可能对您有所帮助。 除数不能为0,因此使用NULLIF将其更改为NULL。
SELECT u.name,u.class,
SUM( markscored) AS TOTAL,
SUM(outof) AS OUTOF
SUM(CASE WHEN subject LIKE 'biology' THEN markscored ELSE 0 END) AS biology,
SUM(CASE WHEN subject LIKE 'chemistry' THEN markscored ELSE 0 END) AS chemistry,
SUM(CASE WHEN subject LIKE 'physics' THEN markscored ELSE 0 END) AS physics,
SUM( markscored)/NULLIF(SUM(outof),0) as percentage
FROM u, marklist m
WHERE u.id=m.userid
GROUP BY u.name,u.class