Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old February 6th, 2003, 04:41 PM
mgregor13 mgregor13 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Sausalito,ca
Posts: 3 mgregor13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry all records for one table , and a count of records based on ID from another

Can anbody help me with the sql for returning all the records in one table, and in each row, a count of how many records with the key id in another table...

english(ish) query:

select * from ci.companyInfo and count(jl.jobListingInvoicespaid) as jobsCount where siteID=1 and ci.companyID = jl.companyID


thanks,

MG

Reply With Quote
  #2  
Old February 6th, 2003, 09:24 PM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 31 m 56 sec
Reputation Power: 8
SELECT COUNT(table1.*), COUNT(table2.id) as jobsCount
FROM table1
INNER JOIN table2
ON table1.companyID = table2.companyID
AND table1.siteID = 1
GROUP BY (table2.id)

Forgive me if my syntax is off.
...

Your request is somewhat unclear but I think this may be what you are asking for. If so, you should create and index for table on the companyID and the siteID and an index on the companyID in table2.
__________________
El éxito consiste en una serie de pequeñas victorias día a día

MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html

Last edited by victorpendleton : February 6th, 2003 at 09:27 PM.

Reply With Quote
  #3  
Old February 6th, 2003, 10:56 PM
mgregor13 mgregor13 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Sausalito,ca
Posts: 3 mgregor13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

Thanks for the attention... I realize my post was not clear... Let me modify what you have sent as if to better describe what I need.

SELECT companyInfo.*, COUNT(jobListings.jobListingInvoicePaid) as jobsCount
FROM companyInfo
INNER JOIN jobListings
ON companyInfo.companyID = jobListings.companyID
AND companyInfo.siteID = 1
GROUP BY (jobListings.companyID)

this returns the dreaded - "is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"

If I remove the companyInfo.* from the select line, it returns the counts of the jobs for each company, that is what I need, but I am trying to do is link the counts of how many jobs a company has. I have a companies table and a jobs table and each job is keyed to the companies ID. So what I hope to get is something like this:

co.coName co.coId jobs(count)joblistings
Microsoft x9re4w 5
Sun 49gti6 27
Mookey

Reply With Quote
  #4  
Old February 7th, 2003, 03:57 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Code:
select companyInfo.*,
 (select count(*) from jobListings
  where companyInfo.companyID = jobListings.companyID)
 from companyInfo
where companyInfo.siteID = 1

Reply With Quote
  #5  
Old February 7th, 2003, 10:32 AM
mgregor13 mgregor13 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Sausalito,ca
Posts: 3 mgregor13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Sweet...

You know, that is about how I thought it would work but I didn't even try to explore nesting of select statements.

This is my first time using this resource (devshed). It works exactly as it should, you post a question, you get the help you need.

Thanks a lot!!! Everyone pat themselves on the back...


MG


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > all records for one table , and a count of records based on ID from another


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway