如何在SQL中的另一个select中编写select语句

时间:2012-03-15 14:58:35

标签: mysql sql

有谁能告诉我这个查询有什么问题? 它在第二个选择

附近给出了语法错误
SELECT b.mc_boxes_idmc_boxes,
          t.idtitles, 
          t.title,
          t.languages_idlanguages,
          MAX(h.idtitle_history),
          MAX(h.edition)
          (SELECT h.preview, h.file WHERE h.idtitle_history = MAX(h.idtitle_history))
                        FROM mc_boxes_has_titles b
                        LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
                        LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
                        WHERE b.mc_boxes_idmc_boxes = 12
                        AND h.edition IS NOT NULL
                        GROUP BY b.mc_boxes_idmc_boxes, idtitles
                        ORDER BY b.sortorder;

3 个答案:

答案 0 :(得分:9)

看起来您在MAX(h.edition)

之后缺少逗号
SELECT b.mc_boxes_idmc_boxes,
          t.idtitles, 
          t.title,
          t.languages_idlanguages,
          MAX(h.idtitle_history),
          MAX(h.edition), 
          (SELECT h.preview, h.file WHERE h.idtitle_history = MAX(h.idtitle_history))
FROM mc_boxes_has_titles b
LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
WHERE b.mc_boxes_idmc_boxes = 12
     AND h.edition IS NOT NULL
GROUP BY b.mc_boxes_idmc_boxes, idtitles
ORDER BY b.sortorder;

除了逗号之外,您在子查询中选择了两个字段

SELECT b.mc_boxes_idmc_boxes,
          t.idtitles, 
          t.title,
          t.languages_idlanguages,
          MAX(h.idtitle_history),
          MAX(h.edition), 
          (SELECT preview FROM title_history WHERE idtitle_history = MAX(h.idtitle_history)),
          (SELECT [file] FROM title_history WHERE idtitle_history = MAX(h.idtitle_history))
FROM mc_boxes_has_titles b
LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
WHERE b.mc_boxes_idmc_boxes = 12
     AND h.edition IS NOT NULL
GROUP BY b.mc_boxes_idmc_boxes, idtitles
ORDER BY b.sortorder;

答案 1 :(得分:0)

添加到bluefeet的答案,您可能想要检查保留字。例如,“File”是sql server中的保留字。

答案 2 :(得分:0)

将其别名为虚拟表。改变像

这样的东西
SELECT b.mc_boxes_idmc_boxes,
          t.idtitles, 
          t.title,
          t.languages_idlanguages,
          MAX(h.idtitle_history),
          MAX(h.edition)
          (SELECT h.preview, h.file WHERE h.idtitle_history = MAX(h.idtitle_history))
                        FROM mc_boxes_has_titles b
                        LEFT JOIN titles t ON b.titles_idtitles = t.idtitles
                        LEFT JOIN title_history h ON h.titles_idtitles = t.idtitles
                        WHERE b.mc_boxes_idmc_boxes = 12
                        AND h.edition IS NOT NULL
                        GROUP BY b.mc_boxes_idmc_boxes, idtitles
                        ORDER BY b.sortorder) as virtual_column_alias;

检查一下。希望这会奏效。