我的SQL语句看起来像那样
SELECT SQL_CALC_FOUND_ROWS q.id, q.question, sb.subject, c.chapter, sc.section, p.paragraph
FROM questions AS q
, subjects AS sb
, sections AS sc
, chapters AS c
, paragraphs AS p
WHERE (sb.id=q.subject AND c.id=q.chapter AND sc.id=q.section AND p.id=q.paragraph) AND (q.author_id=1)
ORDER BY q.id asc
LIMIT 0, 25
如您所见,theese表格通过id相互关联。问题是,我正在subject
列的questions
列而不是subject
表的subjects
列。我在这里缺少什么?
请注意
我提供了主题作为例子。实际上,所有三个部分AS sc,章节AS c,段落AS p都不起作用。我只是受试者。
答案 0 :(得分:2)
显然你的问题中的查询很好,但仍然要挖掘出来使用此语句,看看 subjects_subject 列中出现了什么:
SELECT SQL_CALC_FOUND_ROWS q.id, q.question, sb.subject as subjects_subject, c.chapter, sc.section, p.paragraph
FROM questions AS q
, subjects AS sb
, sections AS sc
, chapters AS c
, paragraphs AS p
WHERE (sb.id=q.subject AND c.id=q.chapter AND sc.id=q.section AND p.id=q.paragraph) AND (q.author_id=1)
ORDER BY q.id asc
LIMIT 0, 25
希望这有帮助。
答案 1 :(得分:1)
我通常显式使用JOIN语法。这有时会为您提供更好的错误输出,以帮助追踪问题。
这是原始格式,因此您可以轻松查看差异。
SELECT SQL_CALC_FOUND_ROWS q.id, q.question, sb.subject, c.chapter, sc.section, p.paragraph
FROM questions AS q,
subjects AS sb,
sections AS sc,
chapters AS c,
paragraphs AS p
WHERE (sb.id=q.subject
AND c.id=q.chapter
AND sc.id=q.section
AND p.id=q.paragraph)
AND (q.author_id=1)
ORDER BY q.id asc
LIMIT 0, 25
这是编辑后的版本 - 请注意WHERE子句参数定义表挂钩在一起的方式是否已作为ON子句移动到表名列表中。这是INNER JOIN语法的一部分,它是:INNER JOIN ON。
SELECT SQL_CALC_FOUND_ROWS q.id, q.question, sb.subject, c.chapter, sc.section, p.paragraph
FROM questions AS q
INNER JOIN subjects AS sb ON sb.id = q.subject
INNER JOIN sections AS sc ON sc.id = q.section
INNER JOIN chapters AS c ON c.id = q.chapter
INNER JOIN paragraphs AS p ON p.id = q.paragraph
WHERE (q.author_id=1)
ORDER BY q.id asc
LIMIT 0, 25
如果您尝试直接针对MySQL运行,会发生什么?