Jump to content


Photo

MySQL query


  • Please log in to reply
4 replies to this topic

#1 joecooper

joecooper
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 28 February 2006 - 01:16 PM

i have this:

$sql = "SELECT * FROM aaform WHERE map1 IN ('$s_map1', '$s_map2', '$s_map3') OR map2 IN ('$s_map1', '$s_map2', '$s_map3') OR map3 IN ('$s_map1', '$s_map2', '$s_map3') ORDER BY honor ASC";

theres nothing wrong with that, it allworks.

i have this string:
$honor
and it needs to equal numbers ($from $to) which could be from:25 to:84 its what ever the user enters.

and i need it to only display the results where honor is between those numbers...

ive tryed this:

$sql = "SELECT * FROM aaform WHERE map1 IN ('$s_map1', '$s_map2', '$s_map3') OR map2 IN ('$s_map1', '$s_map2', '$s_map3') OR map3 IN ('$s_map1', '$s_map2', '$s_map3') AND honor= '>$to < $from' ORDER BY honor ASC";

but that dont work...

im confused as to how i can make it do this.
any help would be good.
if its not clear enough, ill try and make it make more sence :)
Signature:
[/a]
[a href="http://www.planet-so...=1999&lngWId=8" target="_blank"]EzLogin 1.0[/a]
[a href="http://www.essexracers.com" target="_blank"]Essexracers.com[/a]
Msn Messenger: joe@joeyjoe.co.uk

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 28 February 2006 - 01:26 PM

$sql = "SELECT * FROM aaform WHERE (map1 IN ('$s_map1', '$s_map2', '$s_map3') OR map2 IN ('$s_map1', '$s_map2', '$s_map3') OR map3 IN ('$s_map1', '$s_map2', '$s_map3')) AND honor BETWEEN ('$to' , '$from') ORDER BY honor ASC";

I think anyway - long time since i used between. have a look around for it in the mysql manual you should find it somewhere.

follow me on twitter @PHPsycho

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 28 February 2006 - 01:28 PM

for this type of query, it's all in using parenthesis to define your order of precidence. try this and see if it helps:

SELECT * FROM aaform WHERE (map1 IN ('$s_map1', '$s_map2', '$s_map3') OR map2 IN ('$s_map1', '$s_map2', '$s_map3') OR map3 IN ('$s_map1', '$s_map2', '$s_map3')) AND (honor > ($from - 1) AND honor < ($to + 1)) ORDER BY honor ASC

or, you could just use BETWEEN:
SELECT * FROM aaform WHERE (map1 IN ('$s_map1', '$s_map2', '$s_map3') OR map2 IN ('$s_map1', '$s_map2', '$s_map3') OR map3 IN ('$s_map1', '$s_map2', '$s_map3')) AND (honor BETWEEN $to AND $from) ORDER BY honor ASC

notice i used ($from - 1) and ($to + 1) to INCLUDE the to and from numbers. if you don't want those numbers included, just remove the operation.

hope this helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 joecooper

joecooper
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 28 February 2006 - 01:31 PM

thanks! the +1 and -1 things are a bonus, was going to do that :D thanks ever so much!
Signature:
[/a]
[a href="http://www.planet-so...=1999&lngWId=8" target="_blank"]EzLogin 1.0[/a]
[a href="http://www.essexracers.com" target="_blank"]Essexracers.com[/a]
Msn Messenger: joe@joeyjoe.co.uk

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 28 February 2006 - 01:39 PM

[!--quoteo(post=350198:date=Feb 28 2006, 08:31 AM:name=joecooper)--][div class=\'quotetop\']QUOTE(joecooper @ Feb 28 2006, 08:31 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
thanks! the +1 and -1 things are a bonus, was going to do that :D thanks ever so much!
[/quote]

the BETWEEN command is also inclusive, so if you want a little cleaner SQL code, that's probably the way to go.

good luck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users