您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關mysql中運算符的使用示例,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
案例:創建數據表tmp15,其中包含varchar類型的字段note和int類型的字段price。
使用運算符對表tmp15中不同的字段進行運算。
使用邏輯操作符對數據進行邏輯操作。
使用位操作符對數據進行位操作。
首先創建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中運算符的使用示例”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。