PDA

View Full Version : Using WHERE in MySQL query


samlutley
12-22-04, 12:32 PM
If I have two variables:

$start
$end

How would I query entries from a data base with dates between $start and $end

Would it be something like:

SELECT id, date FROM my_db_table WHERE date>=$start && date<=end ORDER BY date ASC

thanks,
sam

Pig
12-22-04, 03:16 PM
Correct, except that it would be $end, not end.

det1smc
12-22-04, 03:24 PM
almost...

the and operator in SQL is "AND", not "&&".
It's also a good idea to use identifiers "`" if you have a filed name like date.

SELECT `id`, `date` FROM my_db_table
WHERE `date`>='$start' AND `date`<='$end' ORDER BY 'date' ASC


You can also use the BETWEEN (http://dev.mysql.com/doc/mysql/en/Comparison_Operators.html) operator. It does the same thing, but it's a little easier to read.

SELECT `id`, `date` FROM my_db_table
WHERE `date` BETWEEN '$start' AND '$end'
ORDER BY 'date' ASC

samlutley
12-22-04, 03:29 PM
thanks

Pig
12-22-04, 03:33 PM
I thought mysql allows &&
http://dev.mysql.com/doc/mysql/en/Logical_Operators.html

det1smc
12-22-04, 03:53 PM
I thought mysql allows &&
http://dev.mysql.com/doc/mysql/en/Logical_Operators.html
Indeed, you are correct, and I am not :confused: .
I've been using SQL (not mySQL) for many years and I've never seen && used in a WHERE.

Thanks

samlutley
12-22-04, 04:21 PM
If I have a string:


$output = " rowspan = \$number "


And then calculate the value of $number (let's say $number = 3)

How do I successfully echo/print out:

rowspan=3

using something to the effect of


echo $output


right now it just prints

rowspan = $number

det1smc
12-22-04, 05:09 PM
$output = " rowspan = \$number "


If you can assign a value to $number before you create the $output variable.

$number=3;
$output = " rowspan =\"$number\"";
echo $output;
//rowspan ="3"


If the value of $number is unkown, you can use this approach.

$output = " rowspan =\"%d\"";

$number=3;
echo sprintf($output,$number);
//rowspan ="3"

Pig
12-22-04, 05:32 PM
mysql is not very strict about complying to standards. That's why you can insert strings into int columns :(

This is perfectly valid to mysql:
INSERT INTO table (some_int_col) VALUES ('3');