Unix Technical Forum

Throwing away a column in subquery

This is a discussion on Throwing away a column in subquery within the MySQL forums, part of the Database Server Software category; --> Hi, of course the following statement doesn't work because the subquery returns a column too much: SELECT * FROM ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:59 AM
=?iso-8859-1?B?QW5kcukgSORuc2Vs?=
 
Posts: n/a
Default Throwing away a column in subquery

Hi,

of course the following statement doesn't work because the subquery
returns a column too much:

SELECT * FROM customers WHERE customer_id IN (SELECT
customer_id,COUNT(*) AS number FROM orders GROUP BY customer_id HAVING
number>1)

("Find all customers with more than one order")

Can I throw away the surplus column that I only need for the HAVING?

Regards,
André

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:00 AM
Paul Lautman
 
Posts: n/a
Default Re: Throwing away a column in subquery

André Hänsel wrote:
> Hi,
>
> of course the following statement doesn't work because the subquery
> returns a column too much:
>
> SELECT * FROM customers WHERE customer_id IN (SELECT
> customer_id,COUNT(*) AS number FROM orders GROUP BY customer_id HAVING
> number>1)
>
> ("Find all customers with more than one order")
>
> Can I throw away the surplus column that I only need for the HAVING?
>
> Regards,
> André


Why throw away the extra column when you can throw away the subselect and
make the whole thing efficient.

SELECT
*
FROM `orders`
GROUP BY `customer_id`
HAVING COUNT(*) > 1


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:00 AM
=?iso-8859-1?B?QW5kcukgSORuc2Vs?=
 
Posts: n/a
Default Re: Throwing away a column in subquery

On 9 Jun., 12:12, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
> André Hänsel wrote:
> > Hi,

>
> > of course the following statement doesn't work because the subquery
> > returns a column too much:

>
> > SELECT * FROM customers WHERE customer_id IN (SELECT
> > customer_id,COUNT(*) AS number FROM orders GROUP BY customer_id HAVING
> > number>1)

>
> > ("Find all customers with more than one order")

>
> > Can I throw away the surplus column that I only need for the HAVING?

>
> > Regards,
> > André

>
> Why throw away the extra column when you can throw away the subselect and
> make the whole thing efficient.
>
> SELECT
> *
> FROM `orders`
> GROUP BY `customer_id`
> HAVING COUNT(*) > 1


And then make one extra query per customer to get the customer names?
Why is that more efficient?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:00 AM
Paul Lautman
 
Posts: n/a
Default Re: Throwing away a column in subquery

André Hänsel wrote:
> On 9 Jun., 12:12, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
>> André Hänsel wrote:
>>> Hi,

>>
>>> of course the following statement doesn't work because the subquery
>>> returns a column too much:

>>
>>> SELECT * FROM customers WHERE customer_id IN (SELECT
>>> customer_id,COUNT(*) AS number FROM orders GROUP BY customer_id
>>> HAVING number>1)

>>
>>> ("Find all customers with more than one order")

>>
>>> Can I throw away the surplus column that I only need for the HAVING?

>>
>>> Regards,
>>> André

>>
>> Why throw away the extra column when you can throw away the
>> subselect and make the whole thing efficient.
>>
>> SELECT
>> *
>> FROM `orders`
>> GROUP BY `customer_id`
>> HAVING COUNT(*) > 1

>
> And then make one extra query per customer to get the customer names?
> Why is that more efficient?


Just do a join, still far more efficienct than a subselect

SELECT
*
FROM `orders` `o`
JOIN `customers` `c` USING(`customer_id`)
GROUP BY `o`.`customer_id`
HAVING COUNT(*) > 1


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 09:22 AM.


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