2012-03-16 59 views
0

我在PHP下面的代碼:MySQL錯誤在嵌套查詢

SELECT (
    SELECT (
     SELECT `forum_posts.id`,`forum_posts.author`,`forum_posts.author_id`, `forum_boards.date`, MIN(`forum_posts.date`) FROM `forum_posts` 
     WHERE `parent` IN 
      (SELECT `id` FROM `forum_boards` WHERE `parent`="'.Oflow(intval((isset($_GET['id']) ? $_GET['id'] : '0'))).'") 
     INNER JOIN `forum_boards` 
     ON `forum_boards.id`=`forum_posts.id` 
     ORDER BY `update_date` DESC 
     LIMIT 1 
     GROUP BY `parent`; 
    ) ORDER BY `order_large`,`order`; 
) UNION (
    SELECT `name`,`id`,`info`,`parent_name` FROM `forum_boards` WHERE `parent`="'.Oflow(intval((isset($_GET['id']) ? $_GET['id'] : '0'))).'" ORDER BY `order_large`,`order 
) 

這是一個腳本來得到一個論壇系統板和職位名單。它應該做的是從表格「板」和「帖子」中獲取數據。然後嘗試在當前正在查看的董事會的子板中找到最新的帖子。然後,它試圖加入「父母」和董事會「ID」togeather,以便可以匹配列,並相應地對帖子進行排序。最後,執行聯合會將新發現的和有序的帖子與實際的論壇數據結合起來。

錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN forum_boards ON forum_boards.id = forum_posts.id ORDER BY' at line 7

的問題是,它不工作!我已經仔細檢查了每一列和表名,並且它們都存在。這是一個非常「alpha」的代碼,所以如果你有任何效率提示,那就太棒了。

回答

0

不能INNER JOIN這是你的查詢試圖做

SELECT (
    SELECT (
     SELECT `forum_posts.id`,`forum_posts.author`,`forum_posts.author_id`, `forum_boards.date`, MIN(`forum_posts.date`) FROM `forum_posts` 
     WHERE `parent` IN 
      (SELECT `id` FROM `forum_boards` WHERE `parent`="'.Oflow(intval((isset($_GET['id']) ? $_GET['id'] : '0'))).'") 
     INNER JOIN `forum_boards` ## This is effectively JOINing within a WHERE clause 
     ON `forum_boards.id`=`forum_posts.id` 
     ORDER BY `update_date` DESC 
     LIMIT 1 
     GROUP BY `parent`; 
    ) ORDER BY `order_large`,`order`; 
) UNION (
    SELECT `name`,`id`,`info`,`parent_name` FROM `forum_boards` WHERE `parent`="'.Oflow(intval((isset($_GET['id']) ? $_GET['id'] : '0'))).'" ORDER BY `order_large`,`order 
) 

您可能需要重新考慮你的查詢一點點的WHERE子句。

+0

那麼你有什麼建議如何做到這一點? – 2012-03-18 07:50:09

+0

@ConnorPeet我不是100%清楚你想達到什麼。也許發佈一些樣本數據和一些樣本輸出來澄清它。 – liquorvicar 2012-03-18 08:34:47