如何使用“BETWEEN”同一列或不同列在一个 sql 语句中使用两次条件

发布于 2024-11-05 17:48:42 字数 250 浏览 0 评论 0原文

这是sql语句只给出一个值

SELECT count(pn1) FROM pledges
WHERE date1
BETWEEN '2011-05-05' AND '2011-06-06'

我想对同一列或同一表中的不同列多次使用“之间”

例如我想在date1中使用它多次或者我想将它用于date1和date2不同范围

注意:我正在使用 java DB Derpy(JDBC-Derby)

this is sql statement give only one value

SELECT count(pn1) FROM pledges
WHERE date1
BETWEEN '2011-05-05' AND '2011-06-06'

i want use "between" more than one time for same column or for different columns in same table

for example i want use it in date1 more than one time or i want use it for date1 and date2 with different range

NOTE:I'm using java DB Derpy(JDBC-Derby)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

满意归宿 2024-11-12 17:48:42

同一列不止一次:

SELECT count(pn1) 
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
       OR date1 BETWEEN '2011-07-05' AND '2011-08-06'

两个不同的列:

SELECT count(pn1) 
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
      AND date2 BETWEEN '2011-05-05' AND '2011-06-06'

编辑:根据评论,也许您正在寻找类似的东西?

SELECT SUM(CASE WHEN date1 BETWEEN '2011-05-05' AND '2011-06-06' THEN 1 ELSE 0) END AS Count1,
       SUM(CASE WHEN date1 BETWEEN '2011-07-05' AND '2011-08-06' THEN 1 ELSE 0) END AS Count2
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
       OR date1 BETWEEN '2011-07-05' AND '2011-08-06'

More than once for the same column:

SELECT count(pn1) 
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
       OR date1 BETWEEN '2011-07-05' AND '2011-08-06'

Two different columns:

SELECT count(pn1) 
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
      AND date2 BETWEEN '2011-05-05' AND '2011-06-06'

EDIT: Based on comment, perhaps you're looking for something like this instead?

SELECT SUM(CASE WHEN date1 BETWEEN '2011-05-05' AND '2011-06-06' THEN 1 ELSE 0) END AS Count1,
       SUM(CASE WHEN date1 BETWEEN '2011-07-05' AND '2011-08-06' THEN 1 ELSE 0) END AS Count2
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
       OR date1 BETWEEN '2011-07-05' AND '2011-08-06'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文