长话短说
为什么 SELECT * FROM title_included = 0
返回一行?
mysql> SELECT * FROM person_task WHERE title_included = 0;
+----+----------+---------+----------+---------------------+----------+------------+-----------+----------+-----------------------+----------------+-------------+----------+---------+--------------+-----------------+-------------------+---------------+---------------------+---------+---------+-------------------+-------------+---------------------+---------------------+
| id | location | keyword | industry | years_of_experience | language | first_name | last_name | zip_code | title_included | title_excluded | title_scope | function | company | company_size | seniority_level | tenure_at_company | total_results | last_result_scraped | elapsed | is_done | last_page_scraped | total_pages | creation_time | scraping_time |
+----+----------+---------+----------+---------------------+----------+------------+-----------+----------+-----------------------+----------------+-------------+----------+---------+--------------+-----------------+-------------------+---------------+---------------------+---------+---------+-------------------+-------------+---------------------+---------------------+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Global IT procurement | 0 | CURRENT | 0 | 0 | C | 0 | 0 | 12 | 12 | 86.5676 | 0 | 1 | 1 | 2019-11-02 16:08:51 | 2019-11-02 15:12:34 |
+----+----------+---------+----------+---------------------+----------+------------+-----------+----------+-----------------------+----------------+-------------+----------+---------+--------------+-----------------+-------------------+---------------+---------------------+---------+---------+-------------------+-------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM person_task WHERE title_included = "0";
Empty set (0.00 sec)
0
SELECT * FROM person_task WHERE title_included = 0
第一个查询返回一行。
“0”
SELECT * FROM person_task WHERE title_included = "0"
第二个查询返回没有行。
表格
这是我的person_task
表结构。 title_included
这里特别是 MEDIUMTEXT
。
mysql> DESCRIBE person_task;
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| location | mediumtext | YES | | NULL | |
| keyword | mediumtext | YES | | NULL | |
| industry | mediumtext | YES | | NULL | |
| years_of_experience | mediumtext | YES | | NULL | |
| language | mediumtext | YES | | NULL | |
| first_name | mediumtext | YES | | NULL | |
| last_name | mediumtext | YES | | NULL | |
| zip_code | mediumtext | YES | | NULL | |
| title_included | mediumtext | YES | | NULL | |
| title_excluded | mediumtext | YES | | NULL | |
| title_scope | mediumtext | YES | | NULL | |
| function | mediumtext | YES | | NULL | |
| company | mediumtext | YES | | NULL | |
| company_size | mediumtext | YES | | NULL | |
| seniority_level | mediumtext | YES | | NULL | |
| tenure_at_company | mediumtext | YES | | NULL | |
| total_results | mediumint(9) | YES | | NULL | |
| last_result_scraped | mediumint(9) | YES | | NULL | |
| elapsed | float | YES | | NULL | |
| is_done | tinyint(4) | YES | | NULL | |
| last_page_scraped | tinyint(4) | YES | | NULL | |
| total_pages | mediumint(9) | YES | | NULL | |
| creation_time | datetime | YES | | NULL | |
| scraping_time | datetime | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
25 rows in set (0.00 sec)
最佳答案
WHERE title_included = 0
您正在将一个字符串与一个整数进行比较。执行此操作时,MySQL 会将字符串转换为数字数据类型。除非您的字符串以数字开头,否则结果为 0
。因此所有记录都匹配。
WHERE title_included = "0"
(note: should be'0'
)
这里你正在做字符串比较。一切如您所愿。
底线:了解您的数据类型;在查询中传递正确的数据类型以避免隐式转换。如果要比较一个字符串,请将它与一个字符串进行比较。
In all other cases, the arguments are compared as floating-point (real) numbers. For example, a comparison of string and numeric operands takes places as a comparison of floating-point numbers.
下面是一个示例,说明当您进行字符串与整数比较时,幕后发生的事情:
select cast('a' as float)
| cast('a' as float) | | -----------------: | | 0 |
select cast('1a' as float)
| cast('1a' as float) | | ------------------: | | 1 |
关于MySQL 选择值 = 0 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672674/