PDA

Click to See Complete Forum and Search --> : mysql query in php


noielen
Feb 1st, 2008, 09:30 AM
hi everyone,

i have a msyql database with 1 table (almost 2,000 records) for now. one of the fields of this table is 'cost'. i want a query that results, like the ff:

Cost
2,000 to 3,000
4,000 to 6,000
7,000 to 12,000
.
.
.
greater than 20,000

this is for my search option list. is it possible? a query that returns like this? maybe, except for the last one line.

any help really appreciated...

KiwiDexter
Feb 2nd, 2008, 08:31 PM
Not an expert.

But would imagine you can either do multiple SQL statements, apparently a bad thing, or use the "ORDER BY cost" to sort the records then use a switch statement to process them individually in php script.

Zefer
Feb 6th, 2008, 07:48 AM
So you want to select things, ONLY if they are a certain amout?
Try this (untested):

$sql = "SELECT * FROM `table` WHERE `price` >= 20000;";
$query = mysql_query($sql, $link)
or die("Error; " . mysql_error());

visualAd
Feb 6th, 2008, 09:11 AM
SELECT count(id), FLOOR(value/1000)*1000 calc FROM test1 GROUP BY calc;
SELECT value, FLOOR(value/1000)*1000 calc FROM test1 ORDER BY value;


A simple reduction function on the value can sort them out into any ranges you like. Just remember the calc column will contain the lower bound of your range and no value will appear for a range if there are no values within the range.