gpt4 book ai didi

java - 日期之间的Mysql不起作用

转载 作者:行者123 更新时间:2023-12-01 00:41:15 25 4
gpt4 key购买 nike

我正在编写以下查询以获取两个日期之间的记录。我正在使用 Mysql 版本 5.5。可能它完全重复我不知道。但是没有答案对我有用,所以我在问。我正在关注最晚日期之后的最短日期。即使它不起作用。

问题:结果集为空。

pstmt=con.prepareStatement("SELECT urlid FROM youtubevideos WHERE lastwatched >=? AND lastwatched <=? order by id desc LIMIT 8");
pstmt.setString(1,previousdate);//14-05-2015
pstmt.setString(2,currentdate);//12-08-2015
rs=pstmt.executeQuery();
while(rs.next())
{
.........
}

但是我得到的结果集是空的。

我的表 youtubevideos 包含记录

  urlid   lastwatched
-------------------
url1 12-08-2015
url2 11-08-2015
url3 08-05-2015
url4
url5 10-08-2015

上面是一些数据。这里的 lastwatchedvarchar 并且 lastwatched 对于一些记录是空的。如果我之前的日期 08-05-2015 表示小于当前日期 (12),则上述查询有效。否则(从 13-05-2015 开始)它不起作用。请提出任何建议。

最佳答案

你为 sql 使用了错误的日期格式:

12-08-2015 // this is the output format

改用yyyy-MM-dd:

2015-08-12 // this is the sql store format

此查询在我的 Mysql 数据库中运行良好:

SELECT * FROM your_table where DATE <= "2015-05-08" AND DATE >= "2015-08-12"

转换字符串:

Date initDate = new SimpleDateFormat("dd-MM-yyyy").parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String parsedDate = formatter.format(initDate);

关于java - 日期之间的Mysql不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com