Utku Dalmaz
Utku Dalmaz

Reputation: 10192

mysql where condition question

is that possible to use WHERE like this;

$no = $_GET['no'];
$query = "SELECT * FROM `numbers` WHERE `myno` - $no = 4";
...

if not, how can i get the rows like the below example ?

EDIT: to be more clear,

$today = date("Y-m-d");
 $query = "SELECT * FROM `dates` WHERE date - $today = 7";

i want to find rows that has date field which is 7 days ago

thx

Upvotes: 1

Views: 134

Answers (4)

jn1kk
jn1kk

Reputation: 5122

$weekAgo = date("Y-m-d", strtotime("-1 week"));

$query = "SELECT * FROM numbers WHERE myno = '$weekAgo';

Upvotes: 0

ThoKra
ThoKra

Reputation: 3029

If the field is a date field

$query = "SELECT * FROM `numbers` WHERE `myno` = DATE_SUB(`myno`, INTERVAL 7 DAY)"

Edit: or I would prefere this

$query = "SELECT * FROM `numbers` WHERE DATEDIFF(NOW(), `myno`) = 7"

Take a look at the date functions

Upvotes: 3

NickZoic
NickZoic

Reputation: 7835

You can do all sorts of expressions in a WHERE clause, but be careful: if you do

SELECT * FROM numbers WHERE myno - ? = 4

it'll not be able to use any indexes, whereas:

SELECT * FROM numbers WHERE myno = 4 + ?

will be able to use an index on myno.

Upvotes: 2

Juanjo Conti
Juanjo Conti

Reputation: 30073

You can do:

$query = "SELECT * FROM `numbers` WHERE `myno` = 4  + $no"

Upvotes: 1

Related Questions