我们准备玩点有趣的:

select 一个数字:

mysql> select 1 from mysql.user;
+---+
| 1 |
+---+
| 1 |
| 1 |
| 1 |
+---+
3 rows in set (0.00 sec) mysql>

select 一个字符串:

mysql> select 'perl6' from mysql.user;
+-------+
| perl6 |
+-------+
| perl6 |
| perl6 |
| perl6 |
+-------+
3 rows in set (0.00 sec) mysql>

这个字符串单/双引号是一样的, 我们可以去掉空格:

mysql> select'perl6'from mysql.user;
+-------+
| perl6 |
+-------+
| perl6 |
| perl6 |
| perl6 |
+-------+
3 rows in set (0.00 sec) mysql>

可以看到正常执行。

那数字行不行呢?

mysql> select888from mysql.user;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'select888from mysql.user' at line 1
mysql>

不行, 那我们再select一个浮点型看看:

mysql> select 0.8 from mysql.user;
+-----+
| 0.8 |
+-----+
| 0.8 |
| 0.8 |
| 0.8 |
+-----+
3 rows in set (0.00 sec) mysql>

小数点前后如果是0, 可以不写:

mysql> select 1. from mysql.user;
+----+
| 1. |
+----+
| 1 |
| 1 |
| 1 |
+----+
3 rows in set (0.00 sec) mysql> select .6 from mysql.user;
+-----+
| .6 |
+-----+
| 0.6 |
| 0.6 |
| 0.6 |
+-----+
3 rows in set (0.00 sec) mysql>

如果是浮点型的话, 我们就可以让这个数字跟后面的关链字连在一起了:

mysql> select.6 from mysql.user;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'select.6 from mysql.user' at line 1
mysql> select .6from mysql.user;
+-----+
| .6 |
+-----+
| 0.6 |
| 0.6 |
| 0.6 |
+-----+
3 rows in set (0.00 sec) mysql>

.0就相当于0.0了, 可以这样写:

mysql> select 0.0 from mysql.user;
+-----+
| 0.0 |
+-----+
| 0.0 |
| 0.0 |
| 0.0 |
+-----+
3 rows in set (0.00 sec) mysql> select .0from mysql.user;
+-----+
| .0 |
+-----+
| 0.0 |
| 0.0 |
| 0.0 |
+-----+
3 rows in set (0.00 sec) mysql>

很好玩吧。

我们再看看别名:

mysql> select 'perl6' as P from mysql.user;
+-------+
| P |
+-------+
| perl6 |
| perl6 |
| perl6 |
+-------+
3 rows in set (0.00 sec) mysql>

as可省略:

mysql> select 'perl6'  P from mysql.user;
+-------+
| P |
+-------+
| perl6 |
| perl6 |
| perl6 |
+-------+
3 rows in set (0.00 sec) mysql>

省略后可写在一起:

mysql> select'perl6'P from mysql.user;
+-------+
| P |
+-------+
| perl6 |
| perl6 |
| perl6 |
+-------+
3 rows in set (0.01 sec) mysql>

那数字能不能起别名呢?也可以:

mysql> select 1 as 'perl6' from mysql.user;
+-------+
| perl6 |
+-------+
| 1 |
| 1 |
| 1 |
+-------+
3 rows in set (0.00 sec) mysql>

结合前面的浮点型与省略as, 可以这样写:

mysql> select .0'perl6'from mysql.user;
+-------+
| perl6 |
+-------+
| 0.0 |
| 0.0 |
| 0.0 |
+-------+
3 rows in set (0.00 sec) mysql>

我们用union select 再看看:

mysql> select .0from mysql.user union select .01;
+------+
| .0 |
+------+
| 0.00 |
| 0.01 |
+------+
2 rows in set (0.00 sec) mysql>

加个条件:

mysql> select .0from mysql.user where user='root' union select .01;
+------+
| .0 |
+------+
| 0.00 |
| 0.01 |
+------+
2 rows in set (0.00 sec) mysql>

再改改:

mysql> select .0from mysql.user where 1. union select 'perl6';
+-------+
| .0 |
+-------+
| 0.0 |
| perl6 |
+-------+
2 rows in set (0.00 sec) mysql>

小数点数字可以跟后面关链字连起来的, 上面说了, 所以, 我们还可以这样:

mysql> select .0from mysql.user where .1union select'perl6';
+-------+
| .0 |
+-------+
| perl6 |
+-------+
1 row in set (0.00 sec) mysql>

0.开头的小数点会转化为整数, 都转为0。

除了where, 还有一个类似的, 叫做having:

mysql> select .0from mysql.user having .0union select'perl6';
+-------+
| .0 |
+-------+
| perl6 |
+-------+
1 row in set (0.00 sec) mysql>

where 跟having可以一起用:

mysql> select .0from mysql.user where .0 having 1 union select'perl6';
+-------+
| .0 |
+-------+
| perl6 |
+-------+
1 row in set (0.00 sec) mysql> select .0from mysql.user where 1.0 having 1 union select'perl6';
+-------+
| .0 |
+-------+
| 0.0 |
| perl6 |
+-------+
2 rows in set (0.00 sec) mysql>

where跟having一起时, 逻辑是从左到右, 先执行前面的where, 再执行后面的having。

但是having只能跟在where后面。

而且你也不能这么写: (select user from mysql.user where user='root' where user='root')

除了where/having类似外, 还有类似的:

union select 
union distinct select
union all select

最后我们来看一下字符串转换为数字:

mysql> select 'a'+1;
+-------+
| 'a'+1 |
+-------+
| 1 |
+-------+
1 row in set, 1 warning (0.00 sec) mysql>

'a'转换为0, 相加结果是1。

提示有警告, 可以用如下命令查看信息:

mysql> select 'a'+1;
+-------+
| 'a'+1 |
+-------+
| 1 |
+-------+
1 row in set, 1 warning (0.00 sec) mysql> show warnings;
+---------+------+---------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'a' |
+---------+------+---------------------------------------+
1 row in set (0.00 sec) mysql>

字符串会被转换为数字, 如果能转换, 就转换为相应的数字, 不能转换就转换为0

mysql> select '1a'+1;
+--------+
| '1a'+1 |
+--------+
| 2 |
+--------+
1 row in set, 1 warning (0.00 sec) mysql> select 'a1'+1;
+--------+
| 'a1'+1 |
+--------+
| 1 |
+--------+
1 row in set, 1 warning (0.02 sec) mysql> select ''+1;
+---------+
| ''+1 |
+---------+
| 124 |
+---------+
1 row in set (0.00 sec) mysql>

我们可以连着一起写:

mysql> select'per16'+.1'test';
+------+
| test |
+------+
| 0.1 |
+------+
1 row in set, 1 warning (0.00 sec) mysql>

最后再来看点好玩的:

mysql> select host from mysql.user;
+-----------+
| host |
+-----------+
| 127.0.0.1 |
| ::1 |
| localhost |
+-----------+
3 rows in set (0.00 sec) mysql> select host from mysql.user where password='a'+'a';
+-----------+
| host |
+-----------+
| localhost |
| 127.0.0.1 |
| ::1 |
+-----------+
3 rows in set, 5 warnings (0.00 sec) mysql> show warnings;
+---------+------+-------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |
| Warning | 1292 | Truncated incorrect DOUBLE value: 'a' |
| Warning | 1292 | Truncated incorrect DOUBLE value: 'a' |
| Warning | 1292 | Truncated incorrect DOUBLE value: '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |
| Warning | 1292 | Truncated incorrect DOUBLE value: '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |
+---------+------+-------------------------------------------------------------------------------+
5 rows in set (0.00 sec) mysql>

password='a'+'a', 字符串'a'转换成数字(因为有个 + 号), 会发生警告信息, 信息中包含用户的密码。

password='a'+'a', 会被转化为整数类型的表达式, 像 2=1+1

看下面的例子:

mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec) mysql> select user()+'';
+-----------+
| user()+'' |
+-----------+
| 0 |
+-----------+
1 row in set (0.00 sec) mysql> select user()=''+'';
+--------------+
| user()=''+'' |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec) mysql> select user()=''+'';
+---------------+
| user()=''+'' |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec) mysql>

本身user()是字符串的, 遇到了左边的 两个字符串相加, 所以右边的user()也将会自动转换为数值类型。

mysql中的单引号/小数点/字符转换为数字/警告信息的更多相关文章

  1. 字符串怎么换行 || 字符串中使用单引号时应该怎么写 || 保留两位小数 || 数字0在if中的意思是false || 什么情况下会会报undefined || null和undefined的区别 ||

    换行的字符串 "This string\nhas two lines" 字符串中使用单引号时应该怎么写 'You\'re right, it can\'t be a quote' ...

  2. 关于Mysql查询带单引号及插入带单引号字符串问题

    1.转为带参数查询 String sql=""select id from student where name='?'; Connection connect = DriverM ...

  3. js、html中的单引号、双引号及其转义使用

    js.html中的单引号.双引号及其转义使用在js中对相关字符做判断或取值的时候很多情况下都会用到这些. ------ 在一个网页中的按钮,写onclick事件的处理代码,不小心写成如下:<in ...

  4. php中的单引号与双引号详解

    一.引号定义字符串 在Php中,通常一个字符串被定义在一对引号中,如: 'I am a string in single quotes'"I am a string in double qu ...

  5. JS 和 HTML 中的单引号与双引号

    JS中的单引号与双引号 HTML中的单引号与双引号很简单,就是两个字符实体: 显示 描述 实体名称 实体编号 " 双引号.引号 " " ' 单引号.撇号 &apo ...

  6. javaScript中的单引号与双引号

    javaScript中的单引号与双引号没有什么区别.但因为xhtml规范要求所有xhtml属性要用双引号括起来.所以在javaScript中使用单引号. var html = '<h2 clas ...

  7. mysql中实现行号,oracle中的rowid

    mysql中实现行号需要用到MYSQL的变量,因为MySql木有rownumber. MYSQL中变量定义可以用 set @var=0 或 set @var:=0 可以用=或:=都可以,但是如果变量用 ...

  8. Oracle中的单引号问题

    SELECT '<a href="javascript:void(0)" onclick="openWyl('''||a.aac001 FROM ac01 a; S ...

  9. [转载]mysql中实现行号,oracle中的rowid

    mysql中实现行号需要用到MYSQL的变量,因为MySql木有rownumber. MYSQL中变量定义可以用 set @var=0 或 set @var:=0 可以用=或:=都可以,但是如果变量用 ...

随机推荐

  1. ErrorUnable to tunnel through proxy. Proxy returns HTTP1.1 400 Bad Reques

    导入项目的时候,一般会出现这种错误,因为我们的gradle版本,不对,所以默认AS导入后,回去下载你需要的gradle,所以很慢, 先打开:项目路径底下的\gradle\wrapper\gradle- ...

  2. [剑指Offer] 65.矩阵中的路径

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...

  3. BZOJ4709 JSOI2011柠檬(动态规划)

    首先要冷静下来发现这仅仅是在划分区间.显然若有相邻的数字相同应当划分在同一区间.还有一个显然的性质是区间的两端点应该相同且选择的就是端点的数.瞬间暴力dp就变成常数极小100002了.可以继续斜率优化 ...

  4. Devc++编译系统分配给int多少字节

    我看的是<C语言程序设计>..谭浩强的PDF版 里面只讲了VC和TC 的,没有Devc++的..(我的是5.10版) 还有这是什么意思? 经过查阅我进行了这样的测试: 得到了这样的结果: ...

  5. 推荐算法相关总结表(包括DM)

    推荐算法总结表 表1 推荐算法分类 个性化推荐算法分类 启发式算法 基于模型 基于内容 TF-IDF 聚类 最大熵 相似度度量 贝叶斯分类 决策树 神经网络 专家系统 知识推理 协同过滤 K近邻 聚类 ...

  6. CF724E Goods transportation 最小割 DP

    照惯例CF的题不放原题链接... 题意:一个序列上有n个点,每个点有权值pi和si.表示这个点一开始有pi个物品,最多可以卖出si个物品,每个点都可以把物品向编号更大的点运输,但是对于i < j ...

  7. POJ3648:Wedding——题解(配2-SAT简易讲解)

    http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在 ...

  8. 欢迎大家收听喜马拉雅,我的主播频道http://m.ximalaya.com/weizhubo/44966139

    关注光荣之路软件技术培训账号,即时收取测试开发技术的免费公开课信息,各大公司测试及开发招聘信息.最新的技术咨询.线下测试 喜马拉雅微电台,每天早晨光荣之路创始人吴老,都会跟大家一起分享测试行业心得体会 ...

  9. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解

    肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...

  10. [zhuan]VMware中bridge方式网络不能上网的解决办法

    http://jingpin.jikexueyuan.com/article/31601.html 安装好VMware 7后,打开原来的虚拟机文件,发现不能上网,原来的Ethernet是设置的Brid ...