943,987 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 5980
  • MySQL RSS
Dec 6th, 2008
0

Using the COUNT function with a subquery in SQL

Expand Post »
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

MySQL Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Dec 6th, 2008
1

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:

sql Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Database Design Question
Next Thread in MySQL Forum Timeline: Help with SQL Query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC