最新消息: VSCode如何用Git来实现可视化管理源代码?
您现在的位置是:群英 > 数据库 > MySQL数据库 >
mysql运算符操作难不难?
PHP中文网发表于 2021-03-15 16:50 次浏览

案例:创建数据表tmp15,其中包含varchar类型的字段note和int类型的字段price。

  • 使用运算符对表tmp15中不同的字段进行运算。
  • 使用逻辑操作符对数据进行逻辑操作。
  • 使用位操作符对数据进行位操作。

(免费学习推荐:mysql视频教程)

首先创建tmp15表,插入一条记录,note值为"Thisisgood",price值为50,SQL语句如下:

mysql> create table tmp15    -> (
    -> note varchar(100),
    -> price int
    -> );Query OK, 0 rows affected (0.13 sec)mysql> into tmp15 values
    -> (
    -> "Thisisgood",50
    -> );
    mysql> insert into tmp15 values
    -> ("Thisisgood",50);Query OK, 1 row affected (0.06 sec)

(1)对表tmp15中的整型数值字段price进行算数运算,SQL语句如下:

mysql> 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 | ------- ------------ ------------ ----------- ----------- ----------- 1 row in set (0.00 sec)

(2)对表tmp15中的整型数值字段price进行比较运算,SQL语句如下:

mysql> 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 | ------- ---------- ---------- ------------- ------------ ------------ ----------- 1 row in set (0.00 sec)

(3)判断price值是否落在30—80区间、返回70、30相比最大的值、判断price是否为in列表(10、20、50、35)中的某个值,SQL语句如下:

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 | ------- ------------------------- ----------------------- ----------------------- 1 row in set (0.00 sec)

(4)对tmp15中的字符串数值字段note进行比较运算,判断表tmp15中note字段是否为空、使用LIKE判断是否以字母"t"开头、使用regexp判断是否以字母“y”结尾、判断是否包含字母“g”或者“m”,SQL语句如下:

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 | ------------ -------------- ---------------- ------------------ -------------------- 1 row in set (0.05 sec)

(5)将price字段值与null、0进行逻辑运算,SQL语句如下:

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)

(6)将price字段值与2、4进行按位与、按位或 操作,并对price进行按位操作,SQL语句如下:

mysql> select price,
    -> price & 2,
    -> price | 4,
    -> ~price from tmp15; ------- ----------- ----------- ---------------------- | price | price & 2 | price | 4 | ~price               | ------- ----------- ----------- ---------------------- |    50 |         2 |        54 | 18446744073709551565 | ------- ----------- ----------- ---------------------- 1 row in set (0.00 sec)

(7)将price字段值分别额左移和右移两位,SQL语句如下:

mysql> select  price,
    -> price<<2,
    -> price>>2
    -> from tmp15; ------- ---------- ---------- | price | price<<2 | price>>2 | ------- ---------- ---------- |    50 |      200 |       12 | ------- ---------- ---------- 1 row in set (0.00 sec)

相关免费学习推荐:mysql数据库(视频)

以上就是mysql练习之2:运算符的使用的详细内容,更多请关注 群英网络其它相关文章!

标签:mysql 运算符
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
下一篇:没有了
相关信息推荐
2021-03-15 16:49:56 关键词:navicat,日志,mysql
摘要:具体方法:1、打开navicat服务器监控工具,开启日志开关;2、定位日志位置,查看日志;3、执行SQL注入语句,查看日志记录。..
2021-03-15 16:49:49 关键词:mysql,创建表
摘要:mysql创建表的方法:首先指定要在CREATE TABLE子句之后创建的表的名称;然后在【column_list】部分指定表的列表;最后需要为engine子句中的表指定存储引擎。..
2021-03-15 16:39:54 关键词:mysql,错误代码1064
摘要:mysql出现错误代码1064解决办法:首先在命令行模式进入mysql;然后进入一个数据库,输入正确代码【SELECT * FROM branch;SELET * FROM branch;】。..
2021-03-15 16:39:46 关键词:MySQL函数
摘要:案例:使用各种函数操作数据,掌握各种函数的作用和使用方法。(1)使用数学函数rand()生成3个10以内的随机整数。(2)使用sin(),con(),tan(),cot()函数计算三角函数值,并将计算结果转换成整数值..
2021-03-15 16:39:08 关键词:MySQL,单表查询
摘要:单表查询指从一张表数据中查询所需的数据。(1)查询所有字段(2)查询指定字段(3)查询指定记录(4)带in关键字的查询(5)带between and的范围的查询(6)带like的字符匹配查询(7)查询空值(8)带and的多..