MySQL 排序错误

发布于 2024-10-03 13:42:21 字数 437 浏览 0 评论 0原文

如果我运行以下查询:

select * from mysql.user order by abcdef;

MySQL 会抛出以下错误:

错误 1054 (42S22):未知列 “订单子句”中的“abcdef”

如果我运行以下类似的查询:

select * from mysql.user order by "abcdef";

MySQL 现在运行查询并忽略 order by 子句(因为 mysql.user 表缺少名为 'abcdef' 的列)。

这是 MySQL 中的错误吗?当短语用引号括起来时,为什么您希望 order by 默默地失败?在不存在的列上运行 order by 时,错误消息不是合适的吗?

If I run the following query:

select * from mysql.user order by abcdef;

MySQL throws the following error:

ERROR 1054 (42S22): Unknown column
'abcdef' in 'order clause'

If I run the following similar query:

select * from mysql.user order by "abcdef";

MySQL now runs the query and disregards the order by clause (since the mysql.user table lacks a column called 'abcdef').

Is this a bug in MySQL? Why would you want the order by to fail silently when the phrase is in quotes? Wouldn't an error message be appropriate when running order by on a non-existent column?

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

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

发布评论

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

评论(3

初与友歌 2024-10-10 13:42:21

它不会忽略它,它按字符串“abcdef”排序,而不是列。它完全按照您的要求进行操作:按任意字符串排序(很可能根本不执行任何操作)。

大多数 RDBMS 不接受按常量排序(这没有意义),但 MySQL 接受。

It doesn't disregard it, it orders by the string "abcdef", not the column. It does exactly what you ask : ordering by some arbitrary string (which most likely does nothing at all).

Most RDBMS doesn't accept to order by a constant (it doesn't make sense), but MySQL does.

太阳公公是暖光 2024-10-10 13:42:21

当您不使用引号时,它会假设您引用的列名不存在

这是正确的功能

When you are not using quotes, it would assume that you are referring to a column name, which does not exist.

This is correct functionallity

酒儿 2024-10-10 13:42:21

order by 期望您想要排序的字段,如果您希望它升序,您可以将 asc 放在它旁边,如果您希望它降序,您可以放置​​如下的 desc

SELECT *
  FROM mysq.user
 ORDER BY username ASC

The order by expects the field you want to order and if you want it ascending you put asc next to it, and if you want it descending you put a desc like as follows

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