2016-07-05 160 views
0

嘗試創建顯示來自WordPress網站的帖子的自定義方式。有問題需要從數據庫中獲得確切的數據。GROUP BY附近的SQL語法錯誤

所以這是我的錯誤:

Error: 
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 'GROUP BY Imgguid' at line 24 

這是我的查詢:

$sql="SELECT Posts.post_title AS Title, ImgURL.guid AS Imgguid, Posts.guid, Posts.post_content 
FROM `wp_posts` AS Posts 
    INNER JOIN `wp_postmeta` AS Meta ON Posts.ID = Meta.post_id 
    INNER JOIN `wp_postmeta` AS Featured ON Posts.ID = Featured.post_id 
    INNER JOIN `wp_postmeta` AS StartF ON Posts.ID = StartF.post_id 
    INNER JOIN `wp_postmeta` AS EndF ON Posts.ID = EndF.post_id 
    INNER JOIN `wp_postmeta` AS ImgID ON Posts.ID = ImgID.post_id 
    INNER JOIN `wp_posts` AS ImgURL ON ImgID.meta_value = ImgURL.ID 
WHERE 
    (Featured.meta_key = '_ecp_custom_22' AND Featured.meta_value = 'yes') 
    AND 
    (StartF.meta_key = '_ecp_custom_23' AND CAST(StartF.meta_value AS DATETIME) <= CURDATE()) 
    AND 
    (EndF.meta_key = '_ecp_custom_27' AND CAST(EndF.meta_value AS DATETIME) >= CURDATE()) 
    AND 
    (ImgURL.post_type = 'attachment') 
    AND 
    (Posts.ID IN (SELECT relationships.object_id FROM `wp_terms` AS terms 
    INNER JOIN `wp_term_taxonomy` AS taxonomy ON terms.term_id = taxonomy.term_id 
    INNER JOIN `wp_term_relationships` AS relationships ON taxonomy.term_taxonomy_id = relationships.term_taxonomy_id 
    WHERE terms.name = 'Sydney')) 

LIMIT 300 
GROUP BY Title;"; 

不知道爲什麼它不喜歡的集團滴滴。謝謝。

+0

** **極限最後出現 – 2016-07-05 03:46:57

+0

'組by'第一則'limit'現在整理 –

+0

感謝 – benikens

回答

2

LIMIT 300這應該在查詢結束。

GROUP BY Title LIMIT 300 
-2
SELECT Posts.post_title AS Title, ImgURL.guid AS Imgguid, Posts.guid, Posts.post_content 
FROM `wp_posts` AS Posts 
    INNER JOIN `wp_postmeta` AS Meta ON Posts.ID = Meta.post_id 
    INNER JOIN `wp_postmeta` AS Featured ON Posts.ID = Featured.post_id 
    INNER JOIN `wp_postmeta` AS StartF ON Posts.ID = StartF.post_id 
    INNER JOIN `wp_postmeta` AS EndF ON Posts.ID = EndF.post_id 
    INNER JOIN `wp_postmeta` AS ImgID ON Posts.ID = ImgID.post_id 
    INNER JOIN `wp_posts` AS ImgURL ON ImgID.meta_value = ImgURL.ID 
WHERE 
    (Featured.meta_key = '_ecp_custom_22' AND Featured.meta_value = 'yes') 
    AND 
    (StartF.meta_key = '_ecp_custom_23' AND CAST(StartF.meta_value AS DATETIME) <= CURDATE()) 
    AND 
    (EndF.meta_key = '_ecp_custom_27' AND CAST(EndF.meta_value AS DATETIME) >= CURDATE()) 
    AND 
    (ImgURL.post_type = 'attachment') 
    AND 
    (Posts.ID IN (SELECT relationships.object_id FROM `wp_terms` AS terms 
    INNER JOIN `wp_term_taxonomy` AS taxonomy ON terms.term_id = taxonomy.term_id 
    INNER JOIN `wp_term_relationships` AS relationships ON taxonomy.term_taxonomy_id = relationships.term_taxonomy_id 
    WHERE terms.name = 'Sydney')) 
GROUP BY Title 
LIMIT 300 
+1

a,格式化。 b,OP已經表示它已被分類。 c,不要轉儲代碼解釋它 – 2016-07-05 04:13:07