用于BETWEEN两个日期的SQL

时间:2015-08-12 17:52:20

标签: php sql

我尝试过研究,但无法弄明白。我的SQL表有一个类型为“date”的列,它将数据结构化为YYYY-MM-DD。如何执行SQL查询以在两个日期之间进行搜索?

如果我将开始 - 结束更改为1 - 999999999,那么它会显示我的所有数据,因此我知道我的查询正在运行。我无法理解我的日期值。

<?php
$start = '2015-01-01'
$end = '2015-12-31'

$query = "SELECT * FROM table WHERE (Week BETWEEN $start AND $end)";

?>

2 个答案:

答案 0 :(得分:1)

尝试改变,

$query = "SELECT * FROM table WHERE (Week BETWEEN $start AND $end)";

$query = "SELECT * FROM table WHERE Week BETWEEN '$start' AND '$end'";

字段$ start和$ end应为日期类型

答案 1 :(得分:0)

你可以这样做

 $start = '2015-01-01';
 $end = '2015-12-31';

$query=  "SELECT * FROM table WHERE Week 
BETWEEN '$start' AND '$end'";

OR

$query= "SELECT * FROM table WHERE Week   
>= '$start'   AND  Week <= '$end'";