Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 93
  • Last Modified:

Query Syntax

I have this query:

 SELECT wp_posts.id, group_concat(wp_postmeta.post_id) as post_id, wp_postmeta.meta_value
 FROM wp_postmeta, wp_posts
 WHERE (wp_postmeta.meta_key = '_VenueState' OR wp_postmeta.meta_key = '_VenueStateProvince') AND wp_posts.post_status = 'publish' 
GROUP BY wp_postmeta.meta_value
ORDER BY wp_postmeta.meta_value ASC

Open in new window


the error I get is this: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'france-am.wp_posts.ID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Any help I can get to rewrite this so it will run is appreciated.
0
lvollmer
Asked:
lvollmer
  • 2
  • 2
1 Solution
 
lvollmerAuthor Commented:
0
 
SharathData EngineerCommented:
 SELECT wp_posts.id, group_concat(wp_postmeta.post_id) as post_id, wp_postmeta.meta_value
 FROM wp_postmeta, wp_posts
 WHERE (wp_postmeta.meta_key = '_VenueState' OR wp_postmeta.meta_key = '_VenueStateProvince') AND wp_posts.post_status = 'publish' 
GROUP BY wp_posts.id, wp_postmeta.meta_value
ORDER BY wp_postmeta.meta_value ASC

Open in new window

0
 
SharathData EngineerCommented:
Or
 SELECT group_concat(wp_postmeta.post_id) as post_id, wp_postmeta.meta_value
 FROM wp_postmeta, wp_posts
 WHERE (wp_postmeta.meta_key = '_VenueState' OR wp_postmeta.meta_key = '_VenueStateProvince') AND wp_posts.post_status = 'publish' 
GROUP BY wp_postmeta.meta_value
ORDER BY wp_postmeta.meta_value ASC

Open in new window

0
 
lvollmerAuthor Commented:
thanks!
0
 
PortletPaulCommented:
In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause.
see: https://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html

As a MySQL users who has (now) experienced this error please be aware that by default MySQL is NON_STANDARD. What has happened is someone has changed the server settings from that default so that the server now requires proper/standard group by queries.

What this means is:

ALL COLUMNS of the select clause need to be repeated in the group by
EXCEPT those using SUM() COUNT() AVG() ... ...  (the aggregation functions)

select col1, col2, col3, col99, sum(abc), count(xyz), avg(gel)
from ...
group by col1, col2, col3, col99

=========================================================
what you should learn to avoid is the default MySQL group by syntax
which would allow this:

select col1, col2, col3, col99, sum(abc), count(xyz), avg(gel)
from ...
group by col1
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now