Labels

Thursday, March 21, 2013

More in MYSQL, Getting a count of rows from a result set

Hi friends,

There were few interesting things I have tried out these days from that this is the first query.

Getting a count of rows from a result set 

It's a simple an easy query Only thing have to do is take the result set query and think of it as a table

E.g. -  You want to get the total number of users who buy apples more than 2 times .You got a table  as records_of_the_month and in that you have  columns which has buyers_name, name_of_product


so in the First Step what I did is get the people who buy apples more than 2 times.

 SELECT buyers_name, count(*) FROM records_of_the_month WHERE name_of _product ='apple' group by  buyers_name having count(*) >2;

In the second step I got the total number of  people who buy apples more than 2 times.

  SELECT count(*)  FROM  (SELECT buyers_name, count(*) FROM records_of_the_month WHERE name_of_product ='apple' 
group by  buyers_name having count(*) >2 ) tt

This is just a simple mysql query I have learned recently There is some more interesting  queries I'll put a other post to that :)

Have Fun !!!!!!!!!!!!!!!!