SQLite 最小/最大奇怪行为

发布于 2025-01-01 12:06:01 字数 837 浏览 0 评论 0原文

我有一个排名表,其架构定义为排名(id,键,值),其中键是主键

sqlite> .schema rank
CREATE TABLE 'rank' ( ID VARCHAR, KEY VARCHAR, VALUE VARCHAR NOT NULL );
CREATE INDEX 'rank_id___djklaf3451jlZZZRFfa___' ON 'rank' ( ID );

sqlite> select * from rank;
-----------------------------------------------------
Tymb-W64uwvM|Tymc8LPQnxBg|5
TymdPRdFpBcE|TymdSsaIFhuI|2
TymdPRdFpBcE|TymdjkGgExcE|3
TymeVf6N1RH4|TymeZGxydCJ8|3
TymeVf6N1RH4|Tymeecz1ORW8|20

sqlite> select id,min(value) from rank group by id;
------------------------------------------------------
Tymb-W64uwvM|5
TymdPRdFpBcE|2
TymeVf6N1RH4|20

sqlite> select id,max(value) from rank group by id;
------------------------------------------------------
Tymb-W64uwvM|5
TymdPRdFpBcE|3
TymeVf6N1RH4|3

正如您所看到的,最小和最大函数的第三个结果都是不正确的。 sqlite版本是3.6.23。

有什么建议吗?

I have a rank table and its schema is defined as rank(id, key,value) where key is the primary key

sqlite> .schema rank
CREATE TABLE 'rank' ( ID VARCHAR, KEY VARCHAR, VALUE VARCHAR NOT NULL );
CREATE INDEX 'rank_id___djklaf3451jlZZZRFfa___' ON 'rank' ( ID );

sqlite> select * from rank;
-----------------------------------------------------
Tymb-W64uwvM|Tymc8LPQnxBg|5
TymdPRdFpBcE|TymdSsaIFhuI|2
TymdPRdFpBcE|TymdjkGgExcE|3
TymeVf6N1RH4|TymeZGxydCJ8|3
TymeVf6N1RH4|Tymeecz1ORW8|20

sqlite> select id,min(value) from rank group by id;
------------------------------------------------------
Tymb-W64uwvM|5
TymdPRdFpBcE|2
TymeVf6N1RH4|20

sqlite> select id,max(value) from rank group by id;
------------------------------------------------------
Tymb-W64uwvM|5
TymdPRdFpBcE|3
TymeVf6N1RH4|3

As you can see the 3rd result for both the min and max functions are incorrect. The sqlite version is 3.6.23.

Any advice?

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

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

发布评论

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

评论(2

温柔少女心 2025-01-08 12:06:01

这是因为 value 是字符类型(特别是 varchar)而不是数字类型。

如果您要对字符数据进行排序,结果如下:

2
20
3
3
5

如果您执行以下命令,您会看到以下结果:

select value from rank order by value asc

如果您希望按数字排序,则应将列定义为数字类型,例如 integer.

It's because value is a character type (specifically varchar) rather than a numeric type.

If you're sorting character data, it comes out as:

2
20
3
3
5

which is what you'd see if you executed:

select value from rank order by value asc

If you want it to sort numerically, the column should be defined as a numeric type, such as integer.

全部不再 2025-01-08 12:06:01

这是因为您输入了第三个参数 VARCHAR。答案是“正确”,因为它是 VARCHAR 排序。如果您输入了第三个参数 INTEGER,您可能会更喜欢结果。即您的 CREATE TABLE 语句应该是:

CREATE TABLE 'rank' ( ID VARCHAR, KEY VARCHAR, VALUE INTEGER NOT NULL );

否则,如果 VALUE 必须保持为 VARCHAR,您应该尝试使用以下 select 语句进行动态转换,但是,正如其他人所说,这对性能来说并不是很好:

SELECT ID, MIN(CAST(VALUE AS INTEGER)) FROM RANK GROUP BY ID;

It's because you've typed your 3rd parameter VARCHAR. The answer is 'correct' because it's VARCHAR sorting. If you had typed your 3rd parameter INTEGER, you would probably like the result much better. i.e. your CREATE TABLE statement should have been:

CREATE TABLE 'rank' ( ID VARCHAR, KEY VARCHAR, VALUE INTEGER NOT NULL );

Otherwise, if the VALUE must stay as VARCHAR you should try the following select statement to cast on the fly, but, as others have said, this is not great for performance:

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