| | |
Using the COUNT function with a subquery in SQL
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
'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?
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)
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?
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)
SELECT COUNT(name) as number_sold FROM beats WHERE availability = 'Sold' 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. ![]() |
Similar Threads
Other Threads in the MySQL Forum
- Previous Thread: Database Design Question
- Next Thread: Help with SQL Query
| Thread Tools | Search this Thread |
amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm data database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier journalism keyword keywords kickfire laptop law legal license licensing linux managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb single sourcecode spotify sql sugarcrm syntax table techsupport thunderbird transparency virtualization





