Bluehost.com Web Hosting $6.95

needing a count field

This is a discussion on needing a count field within the MySQL Database forums, part of the Database Forums category; Hi, Consider if you will, the following: "select count(*) from Bookings where ContactID = x" The ContactID field is ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-15-2006
hughie
 
Posts: n/a
Default needing a count field

Hi,

Consider if you will, the following:

"select count(*) from Bookings where ContactID = x"

The ContactID field is a foreign key from a 'Contacts' table. This is
fine when I know an individual ContactID. When I get a set of
ContactIDs thus:

"select ContactID from Contacts where (etc..)"

How would I get the 2nd query to also get a count as in the 1st? I've
simplified these queries a bit so as to make my question clearer (?)..a
'Contact' might have made a booking and appear in the 'Bookings' table.
I need to find certain contacts and get a count of their bookings at
the same time.

Anyone?

tia,
Hughie

Reply With Quote
  #2 (permalink)  
Old 12-15-2006
Robert Klemme
 
Posts: n/a
Default Re: needing a count field

On 15.12.2006 11:36, hughie wrote:
> Hi,
>
> Consider if you will, the following:
>
> "select count(*) from Bookings where ContactID = x"
>
> The ContactID field is a foreign key from a 'Contacts' table. This is
> fine when I know an individual ContactID. When I get a set of
> ContactIDs thus:
>
> "select ContactID from Contacts where (etc..)"
>
> How would I get the 2nd query to also get a count as in the 1st? I've
> simplified these queries a bit so as to make my question clearer (?)..a
> 'Contact' might have made a booking and appear in the 'Bookings' table.
> I need to find certain contacts and get a count of their bookings at
> the same time.
>
> Anyone?


Use "GROUP BY ContactID" with "count(*)".

robert
Reply With Quote
  #3 (permalink)  
Old 12-16-2006
hughie
 
Posts: n/a
Default Re: needing a count field

> Use "GROUP BY ContactID" with "count(*)".

thanks. for the record my query ended up as:

select c.ContactID, c.ContactName, count(*) as "bookings"
from Contacts c
left join Members m on
c.ContactID = m.ContactID
join Bookings b on
c.ContactID = b.ContactID
where m.ContactID is null
group by b.ContactID
having bookings > 2
order by bookings

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:09 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0