mysql中运算符的使用示例
这篇文章将为大家详细讲解有关mysql中运算符的使用示例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
案例:创建数据表tmp15,其中包含varchar类型的字段note和int类型的字段price。
使用运算符对表tmp15中不同的字段进行运算。
使用逻辑操作符对数据进行逻辑操作。
使用位操作符对数据进行位操作。
首先创建tmp15表,插入一条记录,note值为"Thisisgood",price值为50,SQL语句如下:
mysql>createtabletmp15->(->notevarchar(100),->priceint->);QueryOK,0rowsaffected(0.13sec)mysql>intotmp15values->(->"Thisisgood",50->);mysql>insertintotmp15values->("Thisisgood",50);QueryOK,1rowaffected(0.06sec)
(1)对表tmp15中的整型数值字段price进行算数运算,SQL语句如下:
mysql>selectprice,->price+10,->price-10,->price*2,->price/2,->price%3->fromtmp15;+-------+------------+------------+-----------+-----------+-----------+|price|price+10|price-10|price*2|price/2|price%3|+-------+------------+------------+-----------+-----------+-----------+|50|60|40|100|25.0000|2|+-------+------------+------------+-----------+-----------+-----------+1rowinset(0.00sec)
(2)对表tmp15中的整型数值字段price进行比较运算,SQL语句如下:
mysql>selectprice,->price>10,->price<10,->price!=10,->price=10,->price<=>10,->price<>10->fromtmp15;+-------+----------+----------+-------------+------------+------------+-----------+|price|price>10|price<10|price!=10|price=10|price<=>10|price<>10|+-------+----------+----------+-------------+------------+------------+-----------+|50|1|0|1|0|0|1|+-------+----------+----------+-------------+------------+------------+-----------+1rowinset(0.00sec)
(3)判断price值是否落在30—80区间、返回70、30相比最大的值、判断price是否为in列表(10、20、50、35)中的某个值,SQL语句如下:
mysql>selectprice,->pricebetween30and80,->greatest(price,70,30),->pricein(10,20,50,35)->fromtmp15;+-------+-------------------------+-----------------------+-----------------------+|price|pricebetween30and80|greatest(price,70,30)|pricein(10,20,50,35)|+-------+-------------------------+-----------------------+-----------------------+|50|1|70|1|+-------+-------------------------+-----------------------+-----------------------+1rowinset(0.00sec)
(4)对tmp15中的字符串数值字段note进行比较运算,判断表tmp15中note字段是否为空、使用LIKE判断是否以字母"t"开头、使用regexp判断是否以字母“y”结尾、判断是否包含字母“g”或者“m”,SQL语句如下:
mysql>selectnote,->noteisnull,->notelike't%',->noteregexp'$y',->noteregexp'[gm]'->fromtmp15;+------------+--------------+----------------+------------------+--------------------+|note|noteisnull|notelike't%'|noteregexp'$y'|noteregexp'[gm]'|+------------+--------------+----------------+------------------+--------------------+|Thisisgood|0|1|0|1|+------------+--------------+----------------+------------------+--------------------+1rowinset(0.05sec)
(5)将price字段值与null、0进行逻辑运算,SQL语句如下:
mysql>selectprice,->price&&1,->price&&null,->price||0,->priceand0,->0andnull,->priceornull->fromtmp15;+-------+------------+---------------+------------+-------------+------------+---------------+|price|price&&1|price&&null|price||0|priceand0|0andnull|priceornull|+-------+------------+---------------+------------+-------------+------------+---------------+|50|1|NULL|1|0|0|1|+-------+------------+---------------+------------+-------------+------------+---------------+1rowinset(0.00sec)mysql>selectprice,->!price,->notnull,->pricexor3,->0xornull,->pricexor0->fromtmp15;+-------+--------+----------+-------------+------------+-------------+|price|!price|notnull|pricexor3|0xornull|pricexor0|+-------+--------+----------+-------------+------------+-------------+|50|0|NULL|0|NULL|1|+-------+--------+----------+-------------+------------+-------------+1rowinset(0.00sec)
(6)将price字段值与2、4进行按位与、按位或 操作,并对price进行按位操作,SQL语句如下:
mysql>selectprice,->price&2,->price|4,->~pricefromtmp15;+-------+-----------+-----------+----------------------+|price|price&2|price|4|~price|+-------+-----------+-----------+----------------------+|50|2|54|18446744073709551565|+-------+-----------+-----------+----------------------+1rowinset(0.00sec)
(7)将price字段值分别额左移和右移两位,SQL语句如下:
mysql>selectprice,->price<<2,->price>>2->fromtmp15;+-------+----------+----------+|price|price<<2|price>>2|+-------+----------+----------+|50|200|12|+-------+----------+----------+1rowinset(0.00sec)
关于“mysql中运算符的使用示例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。