我正在尝试一个小时,但我仍然无法弄清楚此查询中的问题是什么。 :/
SELECT * FROM question ORDER BY question_id DESC LIMIT 3 WHERE topic_name = (SELECT * FROM topic WHERE subject_name = 'Maths')
这是错误:
1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法 'WHERE topic_name =附近(SELECT * FROM主题WHERE subject_name = '数学')'在第1行
答案 0 :(得分:7)
你的陈述都是乱的。正确的陈述是:
SELECT *
FROM question
WHERE topic_name = (SELECT * FROM topic WHERE subject_name = 'Maths')
ORDER BY question_id DESC LIMIT 3 ;
但是,由于子查询,这不起作用。也许你的意思是:
SELECT *
FROM question
WHERE topic_name in (SELECT topic_name FROM topic WHERE subject_name = 'Maths')
ORDER BY question_id DESC LIMIT 3 ;
答案 1 :(得分:2)
您的WHERE子句需要在ORDER BY子句之前:
SELECT * FROM question WHERE topic_name IN (SELECT topic_name FROM topic WHERE subject_name = 'Maths') ORDER BY question_id DESC LIMIT 3
答案 2 :(得分:0)
指定哪个列并将结果限制为一个或使用IN运算符
SELECT * FROM question
WHERE topic_name = (SELECT topic FROM topic WHERE subject_name = 'Maths')
ORDER BY question_id DESC LIMIT 1
SELECT * FROM question
WHERE topic_name in(SELECT topic FROM topic WHERE subject_name = 'Maths')
ORDER BY question_id DESC LIMIT 3
实际上这不是最佳建议。改为进行内连接:
SELECT * FROM question q
INNER JOIN topic t
ON q.topic_name = t.topic
WHERE subject_name = 'Maths'
ORDER BY question_id LIMIT 3