Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] More and less than in a query


[SOLVED] More and less than in a query

Reply
  #1 (permalink)  
Old 08-13-08, 06:58 AM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] More and less than in a query

Hi,

This is my query

$sql = "SELECT teamname,id FROM `aacl_team`";
$result = mysql_query($sql) or die (mysql_error());

I want to do something like this though

SELECT teamname,id FROM `aacl_team` WHERE $laddered is less than or more than $points LIMIT 1

So if $points = 5

I need it fetch out the DB the lowest one after 5 so if team 1 has 4 points and team 2 has 1 points then it will fetch team 1 and the same the other way around for nearest highest but limit it to 1 so I only get 2 teams. Then if something goes wrong like all the lowest teams are 5 can we echo out No Teams.

I tried using the > < signs but I guess they dont work in a query. Sorry for the bad explaination.

Any ideas?
Thanks
Reply With Quote
  #2 (permalink)  
Old 08-13-08, 07:03 AM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$sql "SELECT teamname,id FROM `aacl_team` WHERE $currentpointsp > $currentpoints LIMIT 1 AND $currentpointsp < $currentpoints LIMIT 1";
$result mysql_query($sql) or die (mysql_error()); 
Thats the code I tried. Just a random guess, might give you a better understanding of what I whould like to achieve
Reply With Quote
  #3 (permalink)  
Old 08-13-08, 09:58 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Gah, misread your message...
__________________
The toxic ZCE

Last edited by Keith; 08-13-08 at 10:01 AM.
Reply With Quote
  #4 (permalink)  
Old 08-13-08, 02:02 PM
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 might be able to do it with two queries like this:
PHP Code:

<?php
$row1 
"";
$row2 "";
$teams "";
$sql "SELECT teamname,id FROM `aacl_team` WHERE $currentpointsp < $currentpoints ORDER BY $currentpointsp DESC LIMIT 1";
$result mysql_query($sql) or die (mysql_error());
if(
$result)
{
 
$row1 mysql_fetch_assoc($result);
 
mysql_free_result($result);
 }
$sql "SELECT teamname,id FROM `aacl_team` WHERE $currentpointsp > $currentpoints ORDER BY $currentpointsp ASC LIMIT 1 ";
$result mysql_query($sql) or die (mysql_error());
if(
$result)
{
 
$row2 mysql_fetch_assoc($result);
 
mysql_free_result($result);
 }
if(
$row1)
{
 
$teams .= "Id = ".$row1["id"]." Team = ".$row1["teamname"]."<br />";
 if(
$row2){$teams .= "Id = ".$row2["id"]." Team = ".$row2["teamname"]."<br />";}
 }
else{
$teams "No Teams<br />";
echo 
$teams;
?>
This should work, but you may have to play around with the ASC and DESC in the queries.

What happens if you have two or more teams that have the same number of points, lower but closest to the set point?
__________________
Jerry Broughton

Last edited by job0107; 08-13-08 at 02:17 PM.
Reply With Quote
  #5 (permalink)  
Old 08-13-08, 03:02 PM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
What happens if you have two or more teams that have the same number of points, lower but closest to the set point?
Erm will it pick any row at random? That will be allright
Reply With Quote
  #6 (permalink)  
Old 08-13-08, 03:44 PM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Script Fixed.

Last edited by TheCase; 08-13-08 at 03:56 PM.
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


All times are GMT -5. The time now is 07:53 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.