当前位置: 首页 > news >正文

重庆网站推广外包企业百度seo规则

重庆网站推广外包企业,百度seo规则,零起步如何做设计师,必要这个网站怎么样点击上方“匠心零度”,选择“设为星标”做积极的人,而不是积极废人来源 | https://urlify.cn/mMBFZz某一天,开发问我,为什么针对一个查询会有两条记录,且其中一条记录并不符合条件select * from tablea where xxno 17…

点击上方“匠心零度”,选择“设为星标”

做积极的人,而不是积极废人

1aba674e7a73bac8b74c2f415b7e8a32.png

来源 | https://urlify.cn/mMBFZz

某一天,开发问我,为什么针对一个查询会有两条记录,且其中一条记录并不符合条件select * from tablea where xxno = 170325171202362928;xxno为 170325171202362928 和 170325171202362930的都出现在结果中。

一个等值查询为什么会有另外一个不同值的记录查询出来呢?

我们一起来看看究竟!

分析

我们查看该表结构,发现xxno 为varchar 类型,但是等号右边是一个数值类型,这种情况下MySQL会如何进行处理呢?官方文档如下:https://dev.mysql.com/doc/refman/5.6/en/type-conversion.html

The following rules describe how conversion occurs for comparison operations: .... 省略一万字 .... In all other cases, the arguments are compared as floating-point (real) numbers.

也就是说,他会将等于号的两边转换成浮点数来做比较。

Comparisons that use floating-point numbers (or values that are converted to floating-point numbers) are approximate because such numbers are inexact. This might lead to results that appear inconsistent:

如果比较使用了浮点型,那么比较会是近似的,将导致结果看起来不一致,也就是可能导致查询结果错误。

我们测试下刚刚生产的例子:

mysql > select '170325171202362928' = 170325171202362930;
+-------------------------------------------+
| '170325171202362928' = 170325171202362930 |
+-------------------------------------------+
|                                         1 |
+-------------------------------------------+
1 row in set (0.00 sec)```

可以发现,字符串的'170325171202362928' 和 数值的170325171202362930比较竟然是相等的。我们再看下字符串'170325171202362928' 和字符串'170325171202362930' 转化为浮点型的结果

mysql  > select '170325171202362928'+0.0;
+--------------------------+
| '170325171202362928'+0.0 |
+--------------------------+
|    1.7032517120236294e17 |
+--------------------------+
1 row in set (0.00 sec)

mysql > select '170325171202362930'+0.0;
+--------------------------+
| '170325171202362930'+0.0 |
+--------------------------+
|    1.7032517120236294e17 |
+--------------------------+
1 row in set (0.00 sec)

我们发现,将两个不同的字符串转化为浮点数后,结果是一样的,

所以只要是转化为浮点数之后的值是相等的,那么,经过隐式转化后的比较也会相等,我们继续进行测试其他转化为浮点型相等的字符串的结果

mysql > select '170325171202362931'+0.0;
+--------------------------+
| '170325171202362931'+0.0 |
+--------------------------+
|    1.7032517120236294e17 |
+--------------------------+
1 row in set (0.00 sec)

mysql > select '170325171202362941'+0.0;
+--------------------------+
| '170325171202362941'+0.0 |
+--------------------------+
|    1.7032517120236294e17 |
+--------------------------+
1 row in set (0.00 sec)

字符串'170325171202362931''170325171202362941'转化为浮点型结果一样,我们看下他们和数值的比较结果

mysql > select '170325171202362931' = 170325171202362930;
+-------------------------------------------+
| '170325171202362931' = 170325171202362930 |
+-------------------------------------------+
|                                         1 |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql > select '170325171202362941' = 170325171202362930;
+-------------------------------------------+
| '170325171202362941' = 170325171202362930 |
+-------------------------------------------+
|                                         1 |
+-------------------------------------------+
1 row in set (0.00 sec)

结果也是符合预期的。

因此,当MySQL遇到字段类型不匹配的时候,会进行各种隐式转化,一定要小心,有可能导致精度丢失。

For comparisons of a string column with a number, MySQL cannot use an index on the column to look up the value quickly. If str_col is an indexed string column, the index cannot be used when performing the lookup in the following statement:

如果字段是字符型,且上面有索引的话,如果查询条件是用数值来过滤的,那么该SQL将无法利用字段上的索引

SELECT * FROM tbl_name WHERE str_col=1;

The reason for this is that there are many different strings that may convert to the value 1, such as '1', ' 1', or '1a'.

我们进行测试

mysql > create table tbl_name(id int ,str_col varchar(10),c3 varchar(5),primary key(id),key idx_str(str_col));
Query OK, 0 rows affected (0.02 sec)

mysql  > insert into tbl_name(id,str_col) values(1,'a'),(2,'b');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql  > insert into tbl_name(id,str_col) values(3,'3c'),(4,'4d');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql  > desc select * from tbl_name where str_col='a';
+----+-------------+----------+------+---------------+---------+---------+-------+------+--------------------------+
| id | select_type | table    | type | possible_keys | key     | key_len | ref   | rows | Extra                    |
+----+-------------+----------+------+---------------+---------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | tbl_name | ref  | idx_str       | idx_str | 13      | const |    1 | Using where; Using index |
+----+-------------+----------+------+---------------+---------+---------+-------+------+--------------------------+

mysql  > desc select * from tbl_name where str_col=3;
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | tbl_name | ALL  | idx_str       | NULL | NULL    | NULL |    4 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql [localhost] {msandbox} (test) > select * from tbl_name where str_col=3;
+----+---------+------+
| id | str_col | c1   |
+----+---------+------+
|  3 | 3c      | NULL |
+----+---------+------+
1 row in set, 2 warnings (0.00 sec)

同时我们可以看到,我们用数值型的3str_col进行比较的时候,他无法利用索引,同时取出来的值也是错误的,

mysql  > show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '3c' |
| Warning | 1292 | Truncated incorrect DOUBLE value: '4d' |
+---------+------+----------------------------------------+

MySQL针对3c 和 4d这两个值进行了转化,变成了34

小结

在数据库中进行查询的时候,不管是Oracle还是MySQL,一定要注意字段类型,杜绝隐式转化,不仅会导致查询缓慢,还会导致结果错误。

END

如果读完觉得有收获的话,欢迎点【好看】,关注【匠心零度】,查阅更多精彩历史!!!

631b8b055a1e84b737c9b55b766d618b.gif

让我“好看” 6264c8137e5e14c76d9896db8bd9b7cc.gif

http://www.lbrq.cn/news/2503423.html

相关文章:

  • 小企业网站建设公司抚顺网站建设
  • 做封面下载网站网站制作公司怎么样
  • 免费做销售网站查域名
  • 自己做的个人网站无法备案手机制作网页用什么软件
  • 普通电脑怎么做网站服务器吗seo优化操作
  • 阜宁城乡建设局网站自动交换友情链接
  • wordpress网站百度数据营销平台
  • cms建站系统介绍网站推广的方式
  • 网站数据中心的建设企业网站建设流程
  • 制作企业网站页面实训项目中文域名注册官网入口
  • 个人网站备案说明沈阳线上教学
  • 最精品网站建设seo推广如何做
  • 直接IP做网站百度账号登录入口
  • 做黑枸杞的公司网站制作网页的教程
  • 广州网站制作企业谷歌搜索引擎 google
  • 网站开发合同违约责任企业管理培训课程网课
  • 手机维修网站模板推广技巧
  • 做网站有哪些法规没广告的视频播放器app
  • 网站配置系统线上培训课程
  • 网站建设与设计大作业中国搜索引擎有哪些
  • wordpress文章语言切换厦门seo
  • 做平台还是自己做网站凡科建站代理登录
  • 南阳移动端网站制作如何在百度搜索到自己的网站
  • 做图网站被告360收录
  • 网站demo怎么做2022最新国际新闻10条简短
  • wordpress网站管理插件推广项目网站
  • 广州疫情今天最新情况旺道seo优化软件
  • 创建网站做搞笑视频全球疫情最新消息
  • 网站解析教程软文广告经典案例300大全
  • 关于.net网站开发外文书籍网络软文范例
  • 聊聊测试环境不稳定如何应对
  • 前端高级综合搜索组件 SearchBox 使用详解!
  • 【Git】Linux-ubuntu 22.04 初步认识 -> 安装 -> 基础操作
  • 机器学习sklearn:决策树的参数、属性、接口
  • 四、计算机组成原理——第4章:指令系统
  • window显示驱动开发—Direct3D 11 视频设备驱动程序接口 (DDI)