DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   MySQL (http://www.daniweb.com/forums/forum126.html)
-   -   Using the COUNT function with a subquery in SQL (http://www.daniweb.com/forums/thread161058.html)

CodeBoy101 Dec 6th, 2008 3:36 pm
Using the COUNT function with a subquery in SQL
 
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


select name, production_no
from beats
where availability = 'Sold'
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?

darkagn Dec 6th, 2008 6:26 pm
Re: Using the COUNT function with a subquery in SQL
 
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:

select COUNT(name) as number_sold
from beats
where availability = 'Sold'
and p_staffid = 1;


All times are GMT -4. The time now is 1:59 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC