Current location: Hot Scripts Forums » Other Discussions » Database » division in SQL query?


division in SQL query?

Reply
  #1 (permalink)  
Old 06-02-09, 09:33 PM
tbigwood tbigwood is offline
New Member
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
division in SQL query?

hi all

im trying to create a sports standings category, ordering the teams by their winning percentage. my question is: is there a way to sort the teams through the ORDER BY clause and simply add a mathematical equation in it or is it much more complex then that? It must first add the two database columns (wins + loses) then divide the wins, then order the other teams.

team 1 .900
team 2 .800
team 3 .760
etc...


any help?
Reply With Quote
  #2 (permalink)  
Old 06-03-09, 01:36 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You mean something like this:
PHP Code:

<?php
mysql_connect
("host","userName","password");
mysql_select_db("myDb");
$table="myTable";
$results mysql_query("select *, substr(round(wins/(wins+loses),3),-5) as percentage from $table order by percentage");
$output "<table border='1'><tr><th>Teams</th><th>Wins</th><th>Loses</th><th>Winning Percentage</th></tr>";
while(
$row=mysql_fetch_assoc($results))
{
 
$team $row["teams"] ? $row["teams"] : "&nbsp;";
 
$wins $row["wins"] ? $row["wins"] : 0;
 
$loses $row["loses"] ? $row["loses"] : 0;
 
$percentage $row["percentage"] ? $row["percentage"] : 0;
 
$output .= "<tr><td>$team</td><td align='center'>$wins</td><td align='center'>$loses</td><td align='center'>$percentage</td></tr>";
 }
echo 
$output .= "</table>";
?>
Or maybe sort descending:
PHP Code:

<?php
mysql_connect
("host","userName","password");
mysql_select_db("myDb");
$table="myTable";
$results mysql_query("select *, substr(round(wins/(wins+loses),3),-5) as percentage from $table order by percentage desc");
$output "<table border='1'><tr><th>Teams</th><th>Wins</th><th>Loses</th><th>Winning Percentage</th></tr>";
while(
$row=mysql_fetch_assoc($results))
{
 
$team $row["teams"] ? $row["teams"] : "&nbsp;";
 
$wins $row["wins"] ? $row["wins"] : 0;
 
$loses $row["loses"] ? $row["loses"] : 0;
 
$percentage $row["percentage"] ? $row["percentage"] : 0;
 
$output .= "<tr><td>$team</td><td align='center'>$wins</td><td align='center'>$loses</td><td align='center'>$percentage</td></tr>";
 }
echo 
$output .= "</table>";
?>
__________________
Jerry Broughton

Last edited by job0107; 06-03-09 at 02:02 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Query based on last 48 hours? philmckrackon PHP 1 05-07-08 06:56 AM
dynamic where section of sql query soloWebDev Script Requests 3 01-31-08 02:48 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
change my field in this example sal21 ASP 3 07-14-03 02:49 AM


All times are GMT -5. The time now is 05:08 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.