Solved

Query Syntax

Posted on 2016-07-26
5
57 Views
Last Modified: 2016-07-26
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
Comment
Question by:lvollmer
  • 2
  • 2
5 Comments
 

Author Comment

by:lvollmer
ID: 41730377
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41730390
 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
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41730392
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
 

Author Closing Comment

by:lvollmer
ID: 41730598
thanks!
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41730616
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now