Using the COUNT function with a subquery in SQL

Reply

Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Using the COUNT function with a subquery in SQL

 
0
  #1
Dec 6th, 2008
Hi I'm having trouble with a subquery and really need some help.
I'm trying to count to count the number of beats sold by a particular producer.

So the first thing I did was SELECT all the beats sold by that producer so I wrote the code

  1.  
  2. SELECT name, production_no
  3. FROM beats
  4. WHERE availability = 'Sold'
  5. AND p_staffid = 1;

'p_staffid' is the id number for the particular producer and the 'production_no' is the primary key for the beat itself. I have tested this code and it works perfectly.

But how do I count the results?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 787
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Using the COUNT function with a subquery in SQL

 
0
  #2
Dec 6th, 2008
The COUNT function will return the number of rows returned by the query which is what I think you are trying to do. The COUNT function requires a column to count as a parameter, so your query will look something like this:

  1. SELECT COUNT(name) as number_sold
  2. FROM beats
  3. WHERE availability = 'Sold'
  4. AND p_staffid = 1;
Last edited by darkagn; Dec 6th, 2008 at 6:26 pm.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC