| | |
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
Views: 1778 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense aws breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement count court crm data database design developer development drupal ec2 eliminate email enter enterprise error eudora facebook form foss gartner gnu gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keywords kickfire laptop law legal linux maintenance managing matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource operand oracle pdf penelope php priceupdating query referencedesign remove reorderingcolumns resultset saas sharepoint sourcecode spotify sql statement sugarcrm syntax techsupport transparency update virtualization





