嵌入式 SQL 请求
我有一个 SQL 数据库。这里有一个名为 Reports
的表,它看起来像这样,
ID Date Year Title Links
1 2010-05-03 2010 Report 1 link1.php
2 2010-09-03 2010 Report 2 link2.php
3 2011-01-05 2011 Report 3 link3.php
我正在尝试实现它。我想选择 2010-2011 年的所有报告,但 2010 年的报告可能只是 9 月份以来的报告。
现在,我有一条 SQL 语句选择这两年的所有报告,
$sql = "SELECT * FROM Reports WHERE Year = ".$db->escape($jaar1)." OR jaar = ".$db->escape($jaar2);
如何选择Report 2
(因为它的日期是 2010 年 9 月)和Report 3
(因为它可以追溯到 2011 年)?
I have a SQL Database. In here is a table called Reports
and it looks like this
ID Date Year Title Links
1 2010-05-03 2010 Report 1 link1.php
2 2010-09-03 2010 Report 2 link2.php
3 2011-01-05 2011 Report 3 link3.php
What I'm trying to achieve it the this. I want to select all the reports of 2010-2011 but the ones from 2010 may only be those dating from september.
Right now, I've got a SQL statement selecting all the reports of the two years
$sql = "SELECT * FROM Reports WHERE Year = ".$db->escape($jaar1)." OR jaar = ".$db->escape($jaar2);
How can I select Report 2
(cause it dates from september 2010) and Report 3
(cause it dates from 2011)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最简单的方法是使用 <代码>BETWEEN...AND 运算符。
注意反引号。对于
Date
尤其重要,因为它是一个保留字。The easiest way as I see it, is to use the
BETWEEN...AND
operator.Note the backticks. Especially important around
Date
, because it is a reserved word.假设
Date
列的类型为DATE
:Assuming the
Date
column is of typeDATE
:一般模式
general pattern