Unix Technical Forum

Combine GROUP BY with column subquery

This is a discussion on Combine GROUP BY with column subquery within the MySQL forums, part of the Database Server Software category; --> Hi, this won't work, because the subquery may only return one column: DELETE FROM customers WHERE customer_id IN (SELECT ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-29-2008, 09:41 AM
=?ISO-8859-1?B?QW5kcukgSORuc2Vs?=
 
Posts: n/a
Default Combine GROUP BY with column subquery

Hi,

this won't work, because the subquery may only return one column:

DELETE FROM customers WHERE customer_id IN (SELECT customer_id,
SUM(accounting.amount) AS balance FROM customers JOIN accounting USING
(customer_id) HAVING balance > customers.limit)

(Delete all customers with balance > limit.)

How can I do it?

Regards,
André
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-29-2008, 09:41 AM
Paul Lautman
 
Posts: n/a
Default Re: Combine GROUP BY with column subquery

André Hänsel wrote:
> Hi,
>
> this won't work, because the subquery may only return one column:
>
> DELETE FROM customers WHERE customer_id IN (SELECT customer_id,
> SUM(accounting.amount) AS balance FROM customers JOIN accounting USING
> (customer_id) HAVING balance > customers.limit)
>
> (Delete all customers with balance > limit.)
>
> How can I do it?
>
> Regards,
> André


I don't think that is your only problem. You are not grouping by anything,
so your SUM() will fail.

Also, you should be doing this using the Multiple-table syntax with a join,
rather than a sub-query.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-29-2008, 09:41 AM
=?ISO-8859-1?B?QW5kcukgSORuc2Vs?=
 
Posts: n/a
Default Re: Combine GROUP BY with column subquery

On 29 Mai, 00:33, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
>
> I don't think that is your only problem. You are not grouping by anything,
> so your SUM() will fail.


I deleted it when copying to usenet, of course I had:

DELETE FROM customers WHERE customer_id IN (SELECT customer_id,
SUM(accounting.amount) AS balance FROM customers JOIN accounting USING
(customer_id) GROUP BY order_id HAVING balance > customers.limit)

> Also, you should be doing this using the Multiple-table syntax with a join,
> rather than a sub-query.


Some years ago you suggested a join when I asked a similar question.
But because I like subqueries for readability I thought I could fool
you with the DELETE.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-29-2008, 09:41 AM
Captain Paralytic
 
Posts: n/a
Default Re: Combine GROUP BY with column subquery

On 28 May, 22:54, André Hänsel <an...@webkr.de> wrote:
> On 29 Mai, 00:33, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
>
>
>
> > I don't think that is your only problem. You are not grouping by anything,
> > so your SUM() will fail.

>
> I deleted it when copying to usenet, of course I had:
>
> DELETE FROM customers WHERE customer_id IN (SELECT customer_id,
> SUM(accounting.amount) AS balance FROM customers JOIN accounting USING
> (customer_id) GROUP BY order_id HAVING balance > customers.limit)
>
> > Also, you should be doing this using the Multiple-table syntax with a join,
> > rather than a sub-query.

>
> Some years ago you suggested a join when I asked a similar question.
> But because I like subqueries for readability I thought I could fool
> you with the DELETE.


Have you tried moving the SUM(accounting.amount) to the HAVING clause?

But more to the point, whether you like sub-queries or not, they are
NOT the most efficient way to do this. Learn to do JOINs and you will
benefit in lots of ways.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:56 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com