Jump to content
xisto Community
Sign in to follow this  
fsoftball

How Expensive Are Calls To Mysql?

Recommended Posts

Hi,

I would like to sharea design I am currently using and have you all tell me if it is an optimal way of doing things.

First, here is a table (named schedule) in my MySQL database:

game_ID Result
1 W
2 L
3 W
4 T

In my PHP code I want to get the number of wins (W), losses (L), and ties (T). Here is how I did it:

$wins_sql = "SELECT * FROM schedule WHERE result = 'W'";$loss_sql = "SELECT * FROM schedule WHERE result ='L'";$tie_sql = "SELECT * FROM schedule WHERE result ='T'";$num_loss = mysql_num_rows(mysql_query($loss_sql, $conn));$num_wins = mysql_num_rows(mysql_query($wins_sql, $conn));$num_tie = mysql_num_rows(mysql_query($tie_sql, $conn));echo "Overall Record: $num_wins - $num_loss - $num_tie\n";

Are using three different sql statements the best way to do this? If not, can you please provide an example of what would be better? Thanks!

Share this post


Link to post
Share on other sites

I think you want to do something using GROUP BY:

select result, count(*) AS Numberfrom tableGROUP BY resultORDER BY Number;
You don't need Number as a field in your table - you're specifying another value to use in your SQL with the AS command.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.