Unix Technical Forum

union and count

This is a discussion on union and count within the MySQL forums, part of the Database Server Software category; --> Hi all, I've 3 tables. The first is a table of customers, the second a table of orders and ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:04 AM
Bob Bedford
 
Posts: n/a
Default union and count

Hi all,

I've 3 tables. The first is a table of customers, the second a table of
orders and the third a table of questions.

I've to provide a table with the customerid, the number of orders and the
number of questions.
For every order and every question I've a new record in those table. The
tables are quite huge, so I can't send the structure but basically in the 3
tables they are the customerid number to link for.

As I general idea I'd like something like:

select name from customer, count(ordersid) from orders, count (questionid)
from customer
left join orders on customer.customerid = order.customerid
left join questions on customer.customerid = questions.customerid

Is this possible ? how ?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:04 AM
patrice
 
Posts: n/a
Default Re: union and count

"Bob Bedford" <bob@bedford.com> a écrit dans le message de
news:467a93d0$0$3789$5402220f@news.sunrise.ch...
>
> select name from customer, count(ordersid) from orders, count (questionid)
> from customer
> left join orders on customer.customerid = order.customerid
> left join questions on customer.customerid = questions.customerid
>
> Is this possible ? how ?


try to add "group by customerid"
(look at this recent thread "speedest way to handle a select
table1,count(table2) where table1.idtable1=table2.idtable1 ?")


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:04 AM
Bob Bedford
 
Posts: n/a
Default Re: union and count


"patrice" <patrice_labracherie_nospam@free.fr> a écrit dans le message de
news: 467aa3ca$0$19103$426a74cc@news.free.fr...
> "Bob Bedford" <bob@bedford.com> a écrit dans le message de
> news:467a93d0$0$3789$5402220f@news.sunrise.ch...
>>
>> select name from customer, count(ordersid) from orders, count
>> (questionid)
>> from customer
>> left join orders on customer.customerid = order.customerid
>> left join questions on customer.customerid = questions.customerid
>>
>> Is this possible ? how ?

>
> try to add "group by customerid"
> (look at this recent thread "speedest way to handle a select
> table1,count(table2) where table1.idtable1=table2.idtable1 ?")


it works when I've only one count(), but if I've 2 counts from 2 diffent
tables, it multiplies the result of the first count by the result of the
second

I've 2 tables from wich I must do a count and I can't get a correct result.

Bob


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:04 AM
Ross McKay
 
Posts: n/a
Default Re: union and count

On Thu, 21 Jun 2007 17:05:15 +0200, "Bob Bedford" wrote:

>I've 3 tables. The first is a table of customers, the second a table of
>orders and the third a table of questions.
>
>I've to provide a table with the customerid, the number of orders and the
>number of questions.
>For every order and every question I've a new record in those table. The
>tables are quite huge, so I can't send the structure but basically in the 3
>tables they are the customerid number to link for.
>[...]


Using "virtual tables", something like:

Select c.`name`, o.orders, q.questions
From customer c
Join (
Select customerid, count(ordersid) As orders
From orders
Group By customerid
) o On c.customerid = o.customerid,
Join (
Select customerid, count(questionid) As questions
From questions
Group By customerid
) q On c.customerid = q.customerid
Order By c.`name`

NB: just typed it in, you will need to play with it / fix it yourself.
--
Ross McKay, Toronto, NSW Australia
"Let the laddie play wi the knife - he'll learn"
- The Wee Book of Calvin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:05 AM
Bob Bedford
 
Posts: n/a
Default Re: union and count

> Using "virtual tables", something like:
>
> Select c.`name`, o.orders, q.questions
> From customer c
> Join (
> Select customerid, count(ordersid) As orders
> From orders
> Group By customerid
> ) o On c.customerid = o.customerid,
> Join (
> Select customerid, count(questionid) As questions
> From questions
> Group By customerid
> ) q On c.customerid = q.customerid
> Order By c.`name`
>
> NB: just typed it in, you will need to play with it / fix it yourself.
> --
> Ross McKay, Toronto, NSW Australia
> "Let the laddie play wi the knife - he'll learn"
> - The Wee Book of Calvin


Splendid Mate ;-) !!! works fine.

Thanks a lot !


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 04:41 PM.


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