共計 4374 個字符,預計需要花費 11 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這篇文章將為大家詳細講解有關 mysql 中運算符的使用示例,丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
案例:創建數據表 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 中運算符的使用示例”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
向 AI 問一下細節