mysql練習(xí)之2:運算符的使用

mysql練習(xí)之2:運算符的使用

案例:創(chuàng)建數(shù)據(jù)表tmp15,其中包含varchar類型的字段note和int類型的字段price。

  • 使用運算符對表tmp15中不同的字段進行運算。
  • 使用邏輯操作符對數(shù)據(jù)進行邏輯操作。
  • 使用位操作符對數(shù)據(jù)進行位操作。

(免費學(xué)習(xí)推薦:mysql視頻教程


首先創(chuàng)建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中的整型數(shù)值字段price進行算數(shù)運算,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中的整型數(shù)值字段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區(qū)間、返回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中的字符串數(shù)值字段note進行比較運算,判斷表tmp15中note字段是否為空、使用LIKE判斷是否以字母”t”開頭、使用regexp判斷是否以字母“y”結(jié)尾、判斷是否包含字母“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)

相關(guān)免費學(xué)習(xí)推薦:mysql數(shù)據(jù)庫(視頻)

以上就是

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊15 分享