我有一个可以根据需要运行的查询...但是,我想将其转换为临时表...当我添加“ CREATE TEMPORARY TABLE temptable”行时,phpmyadmin中弹出一个错误,提示该子查询返回多于1行。 有什么想法吗?
CREATE TEMPORARY TABLE temptable
SELECT *,
(SELECT judgement FROM wwlpredictedoveralljudgements p1 WHERE p1.pupilID=x.pupilID && p1.subject=x.subject
&& p1.dateAchieved=(SELECT MAX(p2.dateAchieved) FROM wwlpredictedoveralljudgements p2 WHERE p1.pupilID=p2.pupilID && p1.subject=p2.subject
&& p2.dateAchieved>'2018-08-25 00:00:00' && p2.dateAchieved<'2018-12-31 00:00:00')) AS endOfTerm1,
(SELECT judgement FROM wwlpredictedoveralljudgements p4 WHERE p4.pupilID=x.pupilID && p4.subject=x.subject
&& p4.dateAchieved=(SELECT MAX(p5.dateAchieved) FROM wwlpredictedoveralljudgements p5 WHERE p4.pupilID=p5.pupilID && p4.subject=p5.subject
&& p5.dateAchieved>'2018-08-25 00:00:00' && p5.dateAchieved<'2019-03-28 00:00:00')) AS endOfTerm2
FROM (
SELECT u.legalFirstName, u.legalSurname, u.form, p3.pupilID, p3.subject
FROM wwlpredictedoveralljudgements p3
LEFT JOIN users u USING(pupilID)
WHERE u.ncYear<7
GROUP BY p3.pupilID, p3.subject
)x
答案 0 :(得分:0)
如果您的子查询返回的行多,那么您应该或使用IN运算符或将结果减少到限制1之内,例如:
CREATE TEMPORARY TABLE temptable
SELECT *
, ( SELECT judgement
FROM wwlpredictedoveralljudgements p1
WHERE p1.pupilID=x.pupilID
AND p1.subject=x.subject
AND p1.dateAchieved IN (
SELECT MAX(p2.dateAchieved)
FROM wwlpredictedoveralljudgements p2
WHERE p1.pupilID=p2.pupilID
AND p1.subject=p2.subject
AND p2.dateAchieved>'2018-08-25 00:00:00'
AND p2.dateAchieved<'2018-12-31 00:00:00')) AS endOfTerm1
, ( SELECT judgement
FROM wwlpredictedoveralljudgements p4
WHERE p4.pupilID=x.pupilID
AND p4.subject=x.subject
AND p4.dateAchieved IN (
SELECT MAX(p5.dateAchieved)
FROM wwlpredictedoveralljudgements p5
WHERE p4.pupilID=p5.pupilID
AND p4.subject=p5.subject
AND p5.dateAchieved>'2018-08-25 00:00:00'
AND p5.dateAchieved<'2019-03-28 00:00:00')) AS endOfTerm2
FROM (
SELECT u.legalFirstName, u.legalSurname, u.form, p3.pupilID, p3.subject
FROM wwlpredictedoveralljudgements p3
LEFT JOIN users u USING(pupilID)
WHERE u.ncYear<7
GROUP BY p3.pupilID, p3.subject
) x
或
CREATE TEMPORARY TABLE temptable
SELECT *
, ( SELECT judgement
FROM wwlpredictedoveralljudgements p1
WHERE p1.pupilID=x.pupilID
AND p1.subject=x.subject
AND p1.dateAchieved = (
SELECT MAX(p2.dateAchieved)
FROM wwlpredictedoveralljudgements p2
WHERE p1.pupilID=p2.pupilID
AND p1.subject=p2.subject
AND p2.dateAchieved>'2018-08-25 00:00:00'
AND p2.dateAchieved<'2018-12-31 00:00:00'
LIMIT 1 )
LIMIT 1 ) AS endOfTerm1
, ( SELECT judgement
FROM wwlpredictedoveralljudgements p4
WHERE p4.pupilID=x.pupilID
AND p4.subject=x.subject
AND p4.dateAchieved = (
SELECT MAX(p5.dateAchieved)
FROM wwlpredictedoveralljudgements p5
WHERE p4.pupilID=p5.pupilID
AND p4.subject=p5.subject
AND p5.dateAchieved>'2018-08-25 00:00:00'
AND p5.dateAchieved<'2019-03-28 00:00:00'
LIMIT 1 )
LIMIT 1 ) AS endOfTerm2
FROM (
SELECT u.legalFirstName, u.legalSurname, u.form, p3.pupilID, p3.subject
FROM wwlpredictedoveralljudgements p3
LEFT JOIN users u USING(pupilID)
WHERE u.ncYear<7
GROUP BY p3.pupilID, p3.subject
) x