我有一个查询
SELECT * from table1 where sy='$school_year';
和我的
$school_year='2013-2014' //which is a varchar
我该如何制作
$school_year='2012-2013'
我已经尝试了
SELECT * from table1 where sy='$school_year'-1;
但是给我一个错误,说明$ school_year不是整数。我怎样才能获得之前的$ school_year?或者有可能吗?
答案 0 :(得分:1)
或者你也可以这样做:考虑这个例子:
$school_year='2013-2014';
$school_year = explode('-', $school_year);
$school_year[0] = ((int) $school_year[0] - 1);
$school_year[1] = ((int) $school_year[1] - 1);
$school_year = implode('-', $school_year);
$statement = "SELECT * from table1 where sy='$school_year';";
echo $statement;
// Sample
// SELECT * from table1 where sy='2012-2013';
答案 1 :(得分:0)
以下是获取前一个值的一种方法:
select *
from table1
where sy < '$school_year'
order by sy desc
limit 1;
答案 2 :(得分:0)
$year = '2013-2014';
$prev_year = substr($year, 0, 4);
$prev_year = ($prev_year-1) . '-' . $prev_year;
$q = "SELECT * FROM table1 WHERE sy='$prev_year'";
$q
将
SELECT * FROM table1 WHERE sy='2012-2013'
答案 3 :(得分:0)
考虑重组你的表格。您可能的选项(而不是'2013-2014'
格式化的varchar
)是:
int
(最简单,最不容易出错)int4range
(或create your own)和create +
& -
operators(9.2 +)。+
& -
operators代表他们。