嵌入式 SQL 请求

发布于 2024-12-02 23:07:39 字数 652 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久伴你 2024-12-09 23:07:39

我认为最简单的方法是使用 <代码>BETWEEN...AND 运算符。

SELECT * 
FROM `Reports`
WHERE `Date` BETWEEN '2010-09-01' AND NOW()

注意反引号。对于Date尤其重要,因为它是一个保留字。

The easiest way as I see it, is to use the BETWEEN...AND operator.

SELECT * 
FROM `Reports`
WHERE `Date` BETWEEN '2010-09-01' AND NOW()

Note the backticks. Especially important around Date, because it is a reserved word.

无边思念无边月 2024-12-09 23:07:39

假设 Date 列的类型为 DATE

SELECT *
  FROM `Reports`
 WHERE (YEAR(`Date`) = 2010 AND MONTH(`Date`) = 9)
       OR YEAR(`Date`) = 2011

Assuming the Date column is of type DATE:

SELECT *
  FROM `Reports`
 WHERE (YEAR(`Date`) = 2010 AND MONTH(`Date`) = 9)
       OR YEAR(`Date`) = 2011
压抑⊿情绪 2024-12-09 23:07:39

一般模式

where year = 2011
or ( year = 2010 and month = 'September' )

general pattern

where year = 2011
or ( year = 2010 and month = 'September' )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文