Solved

Query Syntax

Posted on 2016-07-26
5
64 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

943 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