mySQL 当 field=0 时返回所有行

发布于 2024-12-12 05:32:08 字数 205 浏览 0 评论 0原文

我正在进行一些测试,当我查询表时,令人惊讶的是,查询 SELECT * FROM table WHERE email=0 返回了表中的所有行。

该表没有“0”值,并且填充了常规电子邮件。

为什么会出现这种情况?这可能会导致严重的安全问题。

有没有办法在不修改查询的情况下避免这种情况?

我在这里错过了什么吗?

谢谢。

I was making some tests, and it was a surprise when i was querying a table, and the query SELECT * FROM table WHERE email=0 returned all rows from the table.

This table has no '0' values and it's populated with regular e-mails.

Why this happens? This can lead to serious security problems.

Is there a way to avoid this without modifying the query?

Am i missing something here?

Thanks.

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

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

发布评论

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

评论(4

怼怹恏 2024-12-19 05:32:08

这是因为它将电子邮件字段(我假设是 varchar 字段)转换为整数。任何没有有效整数的字段都将等于 0。您应该确保仅将字符串字段与字符串值进行比较(日期也是如此,与日期进行比较)。查询应如下所示。

SELECT * FROM table WHERE email='0';

This is because it is converting the email field (which I assume is a varchar field) to an integer. Any field without a valid integer will equate to 0. You should make sure that you only compare string fields to string values (same goes for dates, comparing to dates). The query should be as follows.

SELECT * FROM table WHERE email='0';
提笔落墨 2024-12-19 05:32:08

您的 email 列是 CHARVARCHAR 类型。当您使用条件 email = 0 时,MySQL 会将 email 列的内容转换为整数,以便将它们与您提供的 0 进行比较。如果您将 0 用引号引起来,则查询将按预期工作。 (email = '0')

在 MySQL 中将非数字字符串转换为整数将得到 0。

mysql> SELECT CAST('[email protected]' AS SIGNED);
+-------------------------------------+
| CAST('[email protected]' AS SIGNED) |
+-------------------------------------+
|                                   0 |
+-------------------------------------+

相反,如果您尝试对数字字符串执行相同的操作,它们可能会正确转换:

mysql> SELECT CAST('12345' AS SIGNED);
+-------------------------+
| CAST('12345' AS SIGNED) |
+-------------------------+
|                   12345 |
+-------------------------+

Your email column is a CHAR or VARCHAR type. When you use the condition email = 0, MySQL is casting the contents of the email column to an integer in order to compare them with the 0 you supplied. Had you surrounded your 0 in quotes, the query would work as expected. (email = '0')

Converting a non-numeric string to an integer in MySQL will result in 0.

mysql> SELECT CAST('[email protected]' AS SIGNED);
+-------------------------------------+
| CAST('[email protected]' AS SIGNED) |
+-------------------------------------+
|                                   0 |
+-------------------------------------+

In contrast, if you attempted the same thing with numeric strings, they may cast correctly:

mysql> SELECT CAST('12345' AS SIGNED);
+-------------------------+
| CAST('12345' AS SIGNED) |
+-------------------------+
|                   12345 |
+-------------------------+
安静被遗忘 2024-12-19 05:32:08

电子邮件字段可能是字符,而您正在匹配数字值。

尝试与,

SELECT * FROM table WHERE email='0';

The email field is probably characters and you are matching numeric values.

Try with,

SELECT * FROM table WHERE email='0';
苦行僧 2024-12-19 05:32:08

如果您想获取电子邮件列不应为空或空的所有记录,那么您可以使用

SELECT * FROM table WHERE email IS NOT NULL;

if you want to get all record in which email column should not be blank or empty, then you can use

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