If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Count number of matching entries in separate Table with one query?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-21-09, 20:25
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
Count number of matching entries in separate Table with one query?

Hi, I have the following query.

Code:
  $query = "SELECT #__jt_banners.id
                , #__jt_banners.title
                , #__jt_banners.img_location
                , #__jt_banners.impressions
                , #__jt_banners.start_date
                , #__jt_banners.expiration_date
                , #__jt_banners.published 
          FROM #__jt_banners";
I need to count the number of times each (#__jt_banners.id = #__jt_banner_clicks.banner_id)

Some banner entries will have clicks, some won't. I'd like to limit this to one query if possible, how should I go about doing this?
Reply With Quote
  #2 (permalink)  
Old 12-21-09, 23:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT b.id
     , b.title
     , b.img_location
     , b.impressions
     , b.start_date
     , b.expiration_date
     , b.published 
     , COALESCE(bc.bclicks,0) AS bclicks
  FROM #__jt_banners AS b
LEFT OUTER
  JOIN ( SELECT banner_id
              , COUNT(*) AS bclicks
           FROM #__jt_banner_clicks
         GROUP
             BY banner_id ) AS bc
    ON bc.banner_id = b.id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-22-09, 01:29
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
didn't work
Reply With Quote
  #4 (permalink)  
Old 12-22-09, 02:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Schweppesale View Post
didn't work
and i'm supposed to guess why?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 12-22-09, 05:09
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
Code:
SELECT #__jt_banners.id
                , #__jt_banners.title
                , #__jt_banners.img_location
                , #__jt_banners.impressions
                , #__jt_banners.start_date
                , #__jt_banners.expiration_date
                , #__jt_banners.published
              
                , #__jt_banner_clicks.banner_id
                , COUNT(#__jt_banner_clicks.banner_id) as bann_cnt
          FROM #__jt_banners
            LEFT JOIN #__jt_banner_clicks ON
           (#__jt_banners.id = #__jt_banner_clicks.banner_id)
           GROUP BY #__jt_banners.id
works now. don't worry about it
Reply With Quote
  #6 (permalink)  
Old 12-22-09, 08:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
please, do me the courtesy, since i took the time to help you for free, please at least explain why my query "didn't work"

because i'm pretty sure it did
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On