mysql=====mysql數據運算(2)

1. 案例目的mysql

建立數據表,並對錶中的數據進行運算操做,掌握各類運算符的使用方法。sql


建立表tmp15,其中包含VARCHAR類型的字段note和INT類型的字段price,使用運算符對錶tmp15中不一樣的字段進行運算;使用邏輯操做符對數據進行邏輯操做;使用位操做符對數據進行位操做。ide

本案例使用數據表tmp15,首先建立該表,SQL語句以下:字符串

CREATE TABLE tmp15 (note VARCHAR(100), price INT);it

向表中插入一條記錄,note值爲「Thisisgood」,price值爲50,SQL語句以下:class

INSERT INTO tmp15 VALUES(「Thisisgood」, 50);方法


2. 案例操做過程數據

  對錶tmp15中的整型數值字段price進行算術運算,執行過程以下:di

 SELECT price, price + 10, price -10, price * 2, price /2, price%3 FROM tmp15 ;view

+--------+------------+-----------+-----------+-----------+---------+

| price  | price + 10 | price -10 | price * 2 | price /2 | price%3 |

+--------+------------+-----------+-----------+-----------+---------+

|    50 |      60 |      40 |    100 |  25.0000 |     2 |

+--------+------------+-----------+-----------+-----------+---------+

  對錶tmp15中的整型數值字段price進行比較運算,執行過程以下:

 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 |

+------ -+-----------+-----------+--------------+------------+----------------+--------------+

判斷price值是否落在30~80區間;返回與70,30相比最大的值,判斷price是否爲IN列表(10, 20, 50, 35)中的某個值,執行過程以下:

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 |

+--------+------------------------------------+--------------------------------+----------------------------+

  對tmp15中的字符串數值字段note進行比較運算,判斷表tmp15中note字段是否爲空;使用LIKE判斷是否以字母’t’開頭;使用REGEXP判斷是否以字母’y’結尾;判斷是否包含字母’g’或者’m’,執行過程以下:

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 |

+--------------+-----------------+------------------+----------------------+---------------------------+

  將price字段值與NULL,0進行邏輯運算,執行過程以下:

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)

  將price字段值與二、4進行按位與、按位或操做,並對price進行按位操做,執行過程以下:

mysql> SELECT price, price&2 , price|4, ~price FROM tmp15 ;

+--------+----------+---------+-----------------------------+

| price  | price&2 | price|4 | ~price               |

+--------+----------+---------+-----------------------------+

|    50 |      2 |    54 | 18446744073709551565 |

+--------+----------+---------+------------------------------+

  將price字段值分別左移和右移兩位,執行過程以下:

mysql> SELECT price, price<<2, price>>2  FROM tmp15 ;

+--------+------------+-----------+

| price  | price <<2 | price>>2 |

+--------+------------+-----------+

|    50 |     200 |      12 |

+--------+------------+-----------+

相關文章
相關標籤/搜索