ALTER TABLE ORDER BY str_to_date 和 time_to_sec

发布于 2024-10-08 09:40:07 字数 397 浏览 1 评论 0原文

我试图在导入文件后重新排序表,但在此查询中出现 mysql 语法错误:

ALTER TABLE tablename ORDER BY str_to_date(date, '%m/%d/%Y'), time_to_sec(time) ASC

任何人都可以看到此问题吗?这是我收到的错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(date, '%m/%d/%Y'), time_to_sec(time) ASC' at line 1

I am trying to re-order a table after importing a file but am getting a mysql syntax error on this query:

ALTER TABLE tablename ORDER BY str_to_date(date, '%m/%d/%Y'), time_to_sec(time) ASC

Can anyone see anything wrong with this? this is the error I am getting:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(date, '%m/%d/%Y'), time_to_sec(time) ASC' at line 1

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

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

发布评论

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

评论(5

岛徒 2024-10-15 09:40:07

根据 mysql 手册,ALTER TABLE ORDER BY 需要一个 col_name,但您尝试使用函数对其进行排序。

我建议创建 2 个新列,用函数结果填充它们,然后按它们排序。

According to mysql manual, ALTER TABLE ORDER BY expects a col_name, but you are trying to order it using a function.

I suggest create 2 new columns, populate them with function results, and then order by them.

寄居人 2024-10-15 09:40:07

您可能必须为此使用临时表,因为您是按函数排序的。

CREATE TABLE temp_tablename AS SELECT * FROM tablename;

TRUNCATE tablename;

INSERT INTO tablename SELECT * FROM temp_tablename;

DROP temp_tablename;

您可以将第一个语句设置为CREATE TEMPORARY TABLE,但是如果您丢失会话,您就会丢失数据。

You might have to use a temp table for this since you're ordering by a function.

CREATE TABLE temp_tablename AS SELECT * FROM tablename;

TRUNCATE tablename;

INSERT INTO tablename SELECT * FROM temp_tablename;

DROP temp_tablename;

You could make the first statement a CREATE TEMPORARY TABLE, but if you lose your session you lose your data.

野鹿林 2024-10-15 09:40:07

您是否尝试过

ALTER TABLE tablename ORDER BY date, time ASC

我提到这一点是因为它可能会为您提供所需的顺序。

否则,您需要按照 German Rumm 的建议进行操作,添加具有正确数据类型的列,或者在执行 SQL 时进行排序。

Did you try

ALTER TABLE tablename ORDER BY date, time ASC

I mention this because it might give you the order you need.

Otherwise you'll need to either do as German Rumm suggested, add columns with the correct datatype, or do your ordering when you do your SQL.

假装爱人 2024-10-15 09:40:07

我想我解决了这个问题。我将导入的数据存储在临时表中,然后将其移动到永久表中,并且需要先对临时表中的数据进行排序,然后再插入新表。所以我所做的是通过具有 order by 语句的 select 语句插入到新表中。

I think I resolved this issue. I was storing imported data in a temp table before moving it onto it's permanent table and needed to sort the data in the temp table first before inserting into the new table. so what I do instead is insert into the new table via a select statement which has the order by statement.

花海 2024-10-15 09:40:07

您使用的是较旧版本的 mysql,STR_TO_DATE 仅适用于 MySQL 4.1.1 版本。

更新MySQL版本。

You are using an older version of mysql, STR_TO_DATE is only availabe from version 4.1.1 of MySQL.

Update MySQL version.

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