benikens benikens - 1 year ago 85
SQL Question

SQL syntax error near GROUP BY

Trying to create a custom way of displaying posts from a wordpress site. Having issues getting the exact data I need from the database.

So this is my error:

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


And this is my query:

$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;";


Not sure why it doesn't like the Group By bit. Thanks.

Answer Source

LIMIT 300 This should come at the end of the query.

GROUP BY Title LIMIT 300
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download