导读跟大家讲解下有关mysql练习之2:运算符的使用,相信小伙伴们对这个话题应该也很关注吧,现在就为小伙伴们说说mysql练习之2:运算符的使
跟大家讲解下有关mysql练习之2:运算符的使用,相信小伙伴们对这个话题应该也很关注吧,现在就为小伙伴们说说mysql练习之2:运算符的使用,小编也收集到了有关mysql练习之2:运算符的使用的相关资料,希望大家看到了会喜欢。
案例:创建数据表tmp15其中包含varchar类型的字段note和int类型的字段price。
使用运算符对表tmp15中不同的字段进行运算。使用逻辑操作符对数据进行逻辑操作。
使用位操作符对数据进行位操作。
(免费学习推荐:mysql视频教程)
首先创建tmp15表插入一条记录note值为”Thisisgood”price值为50SQL语句如下:
mysql> create table tmp15 -> ( -> note varchar(100), -> price int -> );Query OK, 0 rows affected (0.13 c)mysql> into tmp15 values -> ( ->平行四边形定理; "Thisisgood",50 -> ); mysql> inrt into tmp15 values -> ("Thisisgood",50);Query OK, 1 row affected (0.06 c)
(1)对表tmp15中的整型数值字段price进行算数运算SQL语句如下:
mysql> lect 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 t (0.00 c)
(2)对表tmp15中的整型数值字段price进行比较运算SQL语句如下:
mysql> lect 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 t (0.00 c)
(3)判断price值是否落在30—80区间、返回70、30相比最大的值、判断price是否为in列表(10、20、50、35)中的某个值SQL语句如下:
mysql> lect 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 t (0.00 c)
(4)对tmp15中的字符串数值字段note进行比较运算判断表tmp15中note字段是否为空、使用LIKE判断是否以字母”t”开头、使用regexp判断是否以字母“y”结尾、判断是否包含字母“g”或者“m”SQL语句如下:
mysql> lect 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 t (0.05 c)
(5)将price字段值与null、0进行逻辑运算SQL语句如下:
mysql> lect 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 t (0.00 c)mysql> lect price, -> !price, -> not null, -> price xor 3, -> 0 xor null, -> price xor 0 -> from tmp15;+-------+--------+----------+-------------+------------+-------------+| price | !price | not null | price xor 3 | 0 xor n童年诗歌ull | price xor 0 |+-------+--------+----------+-------------+------------+-------------+| 50 | 0 | NULL | 0 | NULL | 1 |+-------+--------+----------+-------------+------------+-------------+1 row in t (0.00 c)
(6)将price字段值与2、4进行按位与、按位或 操作并对price进行按位操作SQL语句如下:
mysql> lect price, -> price & 2, -> price | 4, -> ~price from tmp15;+-------+-----------+-----------+----------------------+| price | price & 2 | price | 4 | ~price |+-------+-----------+-----------+----------------------+| 50 | 2 | 54 | 18446744073709551565 |+-------+-----------+-----------+----------------------+1 row in t (0.00 c)
(7)将price字段值分别额左移和右移两位SQL语句如下:
mysql> lect price, -> price<<2, -> price>>2 -> from tmp15;+-------+----------+----------+| price | price<<2 | price>>2 |+-------+----------+----------+| 50 | 200 | 12 |+-------+----------+----------+1 row in t (0.00 c)
开学语录相关免费学习推荐:mysql数据库(视频)
以上就是mysql练习之2:运算符的使用的详细内容!
来源:php中文网
本文发布于:2023-02-24 16:09:17,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/167722615729368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:互联网常识:mysql练习之2:运算符的使用.doc
本文 PDF 下载地址:互联网常识:mysql练习之2:运算符的使用.pdf
留言与评论(共有 0 条评论) |