`SELECT * FROM my_table WHERE textfield >= "whatever";` 返回的行数是否多于 `count("whatever")` 行,因为它是一个数组?

发布于 2024-12-17 20:33:10 字数 316 浏览 0 评论 0 原文

我只是想知道为什么

CREATE TABLE employeeinfo(
  position CHAR(50),
  salary INT NOT NULL,
  branch CHAR(50)
) ENGINE=InnoDB;

SELECT * FROM EMPLOYEEINFO
WHERE position >= 'Regional Manager';

要返回表中的每个人。

是因为它是某种数组吗?这不是按字母顺序排列的事情。难道是因为内存中的值大于区域经理内存中的值?我想有人告诉我这是因为内存中指针位置之间的差异?

I'm just wondering why

CREATE TABLE employeeinfo(
  position CHAR(50),
  salary INT NOT NULL,
  branch CHAR(50)
) ENGINE=InnoDB;

SELECT * FROM EMPLOYEEINFO
WHERE position >= 'Regional Manager';

is returning every person in the table.

Is it because it's some kind of array? It's not an alphabetical thing. Could it be because of the values in memory being greater than Regional Manager's value in memory? I think someone told me it was because of a difference between pointer locations in memory?

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

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

发布评论

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

评论(1

So要识趣 2024-12-24 20:33:10

关系数据库中不存在数组这样的东西。

如果该语句返回每个条目,那是因为条件的计算结果始终为 true。这意味着 position 始终 >= 字符串“Regional Manager”,因此任务是确定为什么会出现这种情况。

如果 position 具有文本类型,则比较是字符串比较,因此诸如 'S' 之类的内容将 >= 'Regional Manager'。字符串比较取决于编码(在MySQL 术语)和用于列的 排序规则。排序规则确定各个字符的顺序。此外,对于大多数情况,MySQL 默认使用不区分大小写的比较排序规则,因此 'a' < “区域经理”,但 's' >=“区域经理”

如果 position 包含一个数值,那么 MySQL 将“区域经理”转换为数字,使用尽可能多的字符串开头可解释为数字的数字:

SELECT CAST( 'Regional Manager' AS SIGNED INT );
-- result: 0
SELECT CAST( '23 Skidoo' AS SIGNED INT );
-- result: 23

如果是这种情况,则只要 position 为积极,它将 >= '区域性 经理'。

There's no such thing as an array in relational databases.

If the statement is returning every entry, it's because the condition always evaluates to true. This implies that position is always >= the string 'Regional Manager', so the task is to determine why this is the case.

If position has a text type, then the comparison is a string comparison, so something such as 'S' would be >= 'Regional Manager'. String comparisons depend on the encoding (called "character set" in MySQL parlance) and collation used for the column. The collation determines the order for individual characters. Additionally, MySQL uses case insensitive comparisons by default for most collations, so 'a' < 'Regional Manager' but 's' >= 'Regional Manager'.

If position holds a numeric value, then MySQL will convert 'Regional Manager' to a number using as much as the beginning of the string as can be interpreted as a number:

SELECT CAST( 'Regional Manager' AS SIGNED INT );
-- result: 0
SELECT CAST( '23 Skidoo' AS SIGNED INT );
-- result: 23

If this is the case, then as long as position is positive, it will be >= 'Regional Manager'.

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