mysql哪里有条件问题

时间:2009-12-18 03:52:32

标签: php mysql

可以使用像这样的WHERE;

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

如果没有,我怎样才能获得如下例所示的行?

编辑: 更清楚,

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

我想找到日期字段为7天前的行

THX

4 个答案:

答案 0 :(得分:3)

如果该字段是日期字段

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

编辑:或者我会优先考虑

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

Take a look at the date functions

答案 1 :(得分:2)

您可以在WHERE子句中执行各种表达式,但要小心:如果您这样做

SELECT * FROM numbers WHERE myno - ? = 4

它将无法使用任何索引,而是:

SELECT * FROM numbers WHERE myno = 4 + ?

将能够在myno上使用索引。

答案 2 :(得分:1)

你可以这样做:

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

答案 3 :(得分:0)

$ weekAgo = date(“Y-m-d”,strtotime(“ - 1周”));

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