本文主要给大家介绍MySQL数据运算讲析,文章内容都是笔者用心摘选和编辑的,mysql数据运算讲析具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下主题内容吧。
创新互联公司专业为企业提供文山州网站建设、文山州做网站、文山州网站设计、文山州网站制作等企业网站建设、网页设计与制作、文山州企业网站模板建站服务,10年文山州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
1. 案例目的
创建数据表,并对表中的数据进行运算操作,掌握各种运算符的使用方法。
创建表tmp15,其中包含VARCHAR类型的字段note和INT类型的字段price,使用运算符对表tmp15中不同的字段进行运算;使用逻辑操作符对数据进行逻辑操作;使用位操作符对数据进行位操作。
本案例使用数据表tmp15,首先创建该表,SQL语句如下:
CREATE TABLE tmp15 (note VARCHAR(100), price INT);
向表中插入一条记录,note值为“Thisisgood”,price值为50,SQL语句如下:
INSERT INTO tmp15 VALUES(“Thisisgood”, 50);
2. 案例操作过程
对表tmp15中的整型数值字段price进行算术运算,执行过程如下:
SELECT price, price + 10, price -10, price * 2, price /2, price%3 FROM tmp15 ;
+--------+------------+-----------+-----------+-----------+---------+
| price | price + 10 | price -10 | price * 2 | price /2 | price%3 |
+--------+------------+-----------+-----------+-----------+---------+
| 50 | 60 | 40 | 100 | 25.0000 | 2 |
+--------+------------+-----------+-----------+-----------+---------+
对表tmp15中的整型数值字段price进行比较运算,执行过程如下:
SELECT price, price> 10, price<10, price != 10, price =10, price <=>10,price <>10 FROM tmp15 ;
+--------+-----------+-----------+--------------+------------+---------------+--------------+
| price | price> 10 | price<10 | price != 10 | price =10 | price <=>10 | price <>10 |
+--------+-----------+-----------+--------------+------------+---------------+--------------+
| 50 | 1 | 0 | 1 | 0 | 0 | 1 |
+------ -+-----------+-----------+--------------+------------+----------------+--------------+
判断price值是否落在30~80区间;返回与70,30相比最大的值,判断price是否为IN列表(10, 20, 50, 35)中的某个值,执行过程如下:
mysql> SELECT price, price BETWEEN 30 AND 80, GREATEST(price, 70,30), price IN (10, 20, 50,35) FROM tmp15 ;
+--------+------------------------------------+--------------------------------+----------------------------+
| price | price BETWEEN 30 AND 80 | GREATEST(price, 70,30) | price IN (10, 20, 50,35) |
+--------+------------------------------------+--------------------------------+----------------------------+
| 50 | 1 | 70 | 1 |
+--------+------------------------------------+--------------------------------+----------------------------+
对tmp15中的字符串数值字段note进行比较运算,判断表tmp15中note字段是否为空;使用LIKE判断是否以字母’t’开头;使用REGEXP判断是否以字母’y’结尾;判断是否包含字母’g’或者’m’,执行过程如下:
mysql> SELECT note, note IS NULL, note LIKE 't%', note REGEXP '$y' ,note REGEXP '[gm]' FROM tmp15 ;
+--------------+----------------+-------------------+-----------------------+--------------------------+
| note | note IS NULL | note LIKE 't%' | note REGEXP '$y' | note REGEXP '[gm]' |
+--------------+----------------+------------------+------------------------+--------------------------+
| Thisisgood | 0 | 1 | 0 | 1 |
+--------------+-----------------+------------------+----------------------+---------------------------+
将price字段值与NULL,0进行逻辑运算,执行过程如下:
mysql> SELECT price, price && 1, price && NULL, price||0, price AND 0, 0 AND NULL, price OR NULL FROM tmp15 ;
+--------+--------------+--------------------+----------+---------------+------------------+--------------------+
| price | price && 1 | price && NULL | price||0 | price AND 0 | 0 AND NULL | price OR NULL |
+--------+--------------+--------------------+----------+---------------+------------------+--------------------+
| 50 | 1 | NULL | 1 | 0 | 0 | 1 |
+--------+--------------+-------------------+-----------+---------------+------------------+--------------------+
1 row in set (0.00 sec)
mysql> SELECT price,!price,NOT NULL,price XOR 3, 0 XOR NULL, price XOR 0 FROM tmp15 ;
+--------+--------+--------------+----------------+-----------------+-----------------+
| price | !price | NOT NULL | price XOR 3 | 0 XOR NULL | price XOR 0 |
+--------+--------+--------------+----------------+-----------------+-----------------+
| 50 | 0 | NULL | 0 | NULL | 1 |
+--------+--------+--------------+----------------+-----------------+-----------------+
1 row in set (0.00 sec)
将price字段值与2、4进行按位与、按位或操作,并对price进行按位操作,执行过程如下:
mysql> SELECT price, price&2 , price|4, ~price FROM tmp15 ;
+--------+----------+---------+-----------------------------+
| price | price&2 | price|4 | ~price |
+--------+----------+---------+-----------------------------+
| 50 | 2 | 54 | 18446744073709551565 |
+--------+----------+---------+------------------------------+
将price字段值分别左移和右移两位,执行过程如下:
mysql> SELECT price, price<<2, price>>2 FROM tmp15 ;
+--------+------------+-----------+
| price | price <<2 | price>>2 |
+--------+------------+-----------+
| 50 | 200 | 12 |
+--------+------------+-----------+
看完以上关于mysql数据运算讲析,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。
当前文章:mysql数据运算讲析
分享路径:http://lswzjz.com/article/pigish.html