Click here to Skip to main content
16,005,473 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-ash3/t1/1743470_234921190024523_1305187537_n.jpg[^]

I wish to count the number of items in this table where column named: "name" = "babydoll"
here is my query:
C#
string query = ("select  count(name) as quantity, name, Product_Category, price, descriptions from Products where name = babydoll");
Posted
Updated 4-Feb-14 2:47am
v2

Select COUNT(name) as NameCount,name from Products where name='babydoll' group by name
 
Share this answer
 
Comments
Kristian Lagaffe 4-Feb-14 8:54am    
Thank you, hiren2. I wish to display as well "Product_Category, price, descriptions" with the same query on my form. When i include these in the query, it gives error. How can i go around it? Do i need 2 separate queries then?
Hiren2 P 4-Feb-14 8:58am    
Group by is an aggregate function. You can use only one column in that group by clause.
You can achieve that using inline query.
try this
SQL
select  name,count(name) as quantity,Product_Category, price, descriptions from Products where name = 'babydoll'
group by name,Product_Category, price, descriptions

but if you need the Product_Category, price, descriptions columns with them then you will get more then one row of result as all the values in these column are not same
if you want by name you can use this
SQL
select  name,count(name) as quantity from Products where name = 'babydoll'
group by name
 
Share this answer
 
Comments
Kristian Lagaffe 4-Feb-14 13:12pm    
Actually i am trying to build an online product catalog. I need to call from the database columns, include (name,Product_Category, price, descriptions), and display them on the products page. Basically all items with same column name "name" definitely have the same values in other columns(...well, its the same item in a certain quantity). So i need to display the quantity available in the database also.
Any idea how i can put this with one single query?

Then repeat for all other items. Each time column "name" is different, call the various values.
You should quote the string in the where clause:
SQL
select  count(name) as quantity, name, Product_Category, price, descriptions from Products where name = 'babydoll'


Please not you are going to get just one record, because you are using count, that is an aggregate operator.
 
Share this answer
 
Comments
Kristian Lagaffe 4-Feb-14 13:12pm    
Actually i am trying to build an online product catalog. I need to call from the database columns, include (name,Product_Category, price, descriptions), and display them on the products page. Basically all items with same column name "name" definitely have the same values in other columns(...well, its the same item in a certain quantity). So i need to display the quantity available in the database also.
Any idea how i can put this with one single query?

Then repeat for all other items. Each time column "name" is different, call the various values.
Actually i am trying to build an online product catalog. I need to call from the database columns, include (name,Product_Category, price, descriptions), and display them on the products page. Basically all items with same column name "name" definitely have the same values in other columns(...well, its the same item in a certain quantity). So i need to display the quantity available in the database also.
Any idea how i can put this with one single query?

Then repeat for all other items. Each time column "name" is different, call the various values.
 
Share this answer
 
In response to the subsequent comments...

You could try something like this
SQL
SELECT P.name, Product_Category, price, description, theCount
FROM Products P
LEFT OUTER JOIN (Select name, COUNT(name) as theCount from Products group by name)
          C ON P.name=C.name

I haven't had a chance to test this, so apologies if there are syntax errors. There is probably a more efficient way to do it as well

[Edit] Thought about this a bit more ...
IF (in capitals deliberately) name, Product_Category, price and description were static then it would make more sense to use
SQL
SELECT name, Product_Category, max(price), description, count(*) from Products GROUP BY name, Product_Category, description
Note that this would only make sense if there had only ever been one price (or if you only wanted the maximum price). The key point (from both solutions) is that if you want to extract other columns you either have to include them in the GROUP BY or do the grouping in a sub-query and return the total/count/max/average that way
 
Share this answer
 
v2
Comments
Kristian Lagaffe 4-Feb-14 17:07pm    
Cool! i correct just one syntax error and i got the count. Is there a way i can request for details of specific value(item name like "babydoll" or "Johnny Bravo" for exple) from the column "Name"? Check the table: https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-ash3/t1/1743470_234921190024523_1305187537_n.jpg
CHill60 4-Feb-14 17:11pm    
Yes ... just put WHERE P.name='babydoll' after the code in my solution above. Note that I've used the table alias P with name to distinguish it from the name column in the inner select.
Kristian Lagaffe 4-Feb-14 17:27pm    
Thousand thanks! ure a Genius! You really saved me. ill appreciate if we could be cool mates. You cud hold me hand and guide me...lol... im only 3weeks old in asp.net and i need a cool programmer like you to hold me my hand. Well i hope u dont mine. i have skype:
" ASPcode_gaffe ". If u have another way i can connect with you, ill appreciate.
CHill60 5-Feb-14 4:41am    
Sorry Kristian - CodeProject is the only way I connect and I already have enough mentees to keep me busy. Keep posting questions here when you get stuck - as you can see there are several people ready to help.
Do try to put as much relevent detail into your questions as possible and always include the code that is giving you a problem (as you have done here) and the text of any error messages you get. Good Luck, see you in the forums.
Kristian Lagaffe 5-Feb-14 9:30am    
Thank you
VB
I wish to thank everyone for your solution. None of the the solutions failed. Just that Mr CHill60 responded to some very specific criteria that i need. i have 3 weeks experience in ASP.net programming and wish to connect with good programmers out there like you, you and you.
im on skype: "ASPcode_gaffe". One more time, Thanks to y'all!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900