2012-12-10 99 views
1

我收到一個MySQL錯誤這個代碼ExpressionEngine 1.6.4(很老的版本)ExpressionEngine查詢模塊和訂購SQL UNION

{exp:query limit="10" paginate="bottom" 
    sql="SELECT 'gallery' AS `content_type`, `cat_id` AS `entry_id`, `recent_entry_date` AS `entry_date` 
     FROM `exp_gallery_categories` 
     WHERE `gallery_id` = 9 AND total_files > 0 
     UNION 
     SELECT 'video' AS `content_type`, `entry_id`, `entry_date` 
     FROM `exp_weblog_titles` 
     WHERE `weblog_id` = 6 ORDER BY `entry_date` DESC" 

    } 
<p>{content_type} - {entry_id} - {entry_date format="%d %F %Y"}</p> 
{paginate}{pagination_links}{/paginate} 
{/exp:query} 

的MySQL返回此:

MySQL ERROR: Error Number: 1054 
Description: Unknown column 'entry_date' in 'order clause' 
Query: SELECT COUNT(*) AS count FROM `exp_gallery_categories` WHERE `gallery_id` = 9 AND total_files > 0 UNION SELECT COUNT(*) AS count FROM `exp_weblog_titles` WHERE `weblog_id` = 6 ORDER BY `entry_date` DESC 

似乎像EE在MySQL之前修改我的查詢並使其失敗(在SQL客戶端上,這可以順利運行)。

+0

FYI:現在有一個EE具體的S大頭釘交換站點:http://expressionengine.stackexchange.com。您可以在那裏發佈未來EE相關的問題。 –

+0

如果我刪除'{paginate} {pagination_links} {/ paginate}'一切正常,但我得到所有結果並失去分頁。 – Angus

回答

0

我假設您的選擇查詢工作。嘗試下面:

sql="SELECT * FROM 
     (SELECT gallery AS content_type, cat_id AS entry_id, 
      recent_entry_date AS entry_date 
      FROM exp_gallery_categories 
      WHERE gallery_id = 9 AND total_files > 0 
      UNION 
      SELECT video AS content_type, entry_id, entry_date 
      FROM exp_weblog_titles 
      WHERE weblog_id= 6 
     ) AS TEMP 
     ORDER BY entry_date DESC" 

編輯:統計查詢:

  SELECT COUNT(*) AS count 
     FROM 
      (SELECT COUNT(*) AS count, 
       recent_entry_date AS entry_date 
       FROM exp_gallery_categories 
       WHERE gallery_id = 9 AND total_files > 0 
       UNION 
       SELECT COUNT(*) AS count, 
       entry_date 
       FROM exp_weblog_titles 
       WHERE weblog_id = 6 
      ) AS TEMP 
     ORDER BY entry_date DESC 
+0

同樣的錯誤,但更改結果查詢: 查詢:SELECT COUNT(*)AS count FROM(SELECT COUNT(*)AS count FROM'exp_gallery_categories' WHERE'gallery_id' = 9 AND total_files> 0 UNION SELECT COUNT(*)AS count FROM 'exp_weblog_titles' WHERE'weblog_id' = 6)作爲TEMP ORDER BY'entry_date' DESC – Angus

+0

@Angus有兩個問題:1.你的單個select查詢是否正常運行? 2.嘗試刪除查詢中的所有引號,因爲看起來不像單引號並嘗試(我更新了答案)? –

+0

@Angus在你的計數查詢中,我看到你從選擇列表中刪除了'entry_date',因此它給出了正確的錯誤。我發佈了修改後的查詢。請嘗試。 –