邏輯運算
not 表示 非邏輯!not null 傳回 的仍是null
mysql> select not 1,not 0, not null;
+-------+-------+----------+
| not 1 | not 0 | not null |
| 0 | 1 | null |
1 row in set (0.00 sec)
and 與 邏輯 任何 與null的邏輯運算的結果都是 null!
mysql> select (1 and 0) ,( 0 and 1 ) ,( 2 and 2 ) ,( 1 and null );
+-----------+-------------+-------------+----------------+
| (1 and 0) | ( 0 and 1 ) | ( 2 and 2 ) | ( 1 and null ) |
| 0 | 0 | 1 | null |
mysql> select (1 and 0) ,( 0 and 1) ,(2 and 2), (1 and null) ,(null and null);
+-----------+------------+-----------+--------------+-----------------+
| (1 and 0) | ( 0 and 1) | (2 and 2) | (1 and null) | (null and null) |
| 0 | 0 | 1 | null | null |
or 邏輯或 運算
mysql> select (1 or 0) ,( 0 or 1) ,(2 or 2), (1 or null) ,(null or null);
+----------+-----------+----------+--------------+----------------+
| (1 or 0) | ( 0 or 1) | (2 or 2) | (1 or null) | (null or null) |
| 1 | 1 | 1 | 1 | null |
1 row in set (0.01 sec)
xor 異或運算。
mysql> select 1 xor 1 ,0 xor 1 ,0 xor 0 , 1 xor null,0 xor null;
+---------+---------+----------+------------+------------+
| 1 xor 1 | 0 xor 1 | 0 xor 0 | 1 xor null | 0 xor null |
| 0 | 1 | 0 | null | null |
mysql> select 2&5; 0010&0101
+-----+
| 2&5 |
| 0 |
mysql> select 5&5;
| 5&5 |
| 5 |
mysql> select 2|3; 或 運算或運算對操作數的二進制做或運算
| 2|3 |
| 3 |
或運算 對操作數的二進制做與運算
mysql> select 2&3&4; 0010&0011&0100
+-------+
| 2&3&4 |
| 0 |
mysql> select 2^3; 位異或運算 對操作數的二進制做異或
| 2^3 |
| 1 |
mysql> select ~1; 位 取反!
+----------------------+
| ~1 |
| 18446744073709551614 |
mysql> select 1000 >>3; -右移位操作!除以2的3次方
+----------+
| 1000 >>3 |
| 125 |
mysql> select 1000 <<3;左移位操作!乘以2的3次方
| 1000 <<3 |
| 8000 |
--常用函數。
在預設狀态下, 在函數和緊随其後的括号之間不得存在空格。這能幫助 mysql 分析程式區分一些同函數名相同的函數調用以及表或列。不過,函數自變量周圍允許有空格出現。
--目前使用的資料庫
mysql> select database();
+------------+
| database() |
| test |
--目前的資料庫版本
mysql> select version();
+----------------+
| version() |
| 5.1.7-beta-log |
1 row in set (0.02 sec)
-目前的登入使用者
mysql> select user();
| user() |
--傳回主機ip位址的數字
mysql> select inet_aton('192.168.12.128');
+-----------------------------+
| inet_aton('192.168.12.128') |
| 3232238720 |
--傳回數字代表的ip
mysql> select inet_ntoa(3232238720);
+-----------------------+
| inet_ntoa(3232238720) |
| 192.168.12.128 |
---傳回字元串密碼的加密版
mysql> select password('12356');
+-------------------------------------------+
| password('12356') |
| *dc594838253636aa6e73a5366878f6f0502bdc5d |
--字元串函數:
字元串指用單引号(‘'’)或雙引号(‘"’)引起來的字元序列。例如:
'a string'
"another string"
如果sql伺服器模式啟用了nsi_quotes,可以隻用單引号引用字元串。用雙引号引用的字元串被解釋為一個識别符。
--連接配接字元
mysql> select concat('aa','bb','yangqilong'),concat('yangql' ,null);
+--------------------------------+------------------------+
| concat('aa','bb','yangqilong') | concat('yangql' ,null) |
| aabbyangqilong | null |
---insert(str,x,y ,insrt)将字元串從第x位置開始,y個字元替換為instr
mysql> select insert ('beijing2008iloveyou',12,3,'me');
+------------------------------------------+
| insert ('beijing2008iloveyou',12,3,'me') |
| beijing2008meveyou |
--轉換大小寫
mysql> select lower('yangqilong') ,upper('yangqilong');
+---------------------+---------------------+
| lower('yangqilong') | upper('yangqilong') |
| yangqilong | yangqilong |
--left(str,x)傳回字元串最左邊的x個字元
--right(str,x)傳回字元串最右邊的x個字元
mysql> select left('yangqllovemysql',6), left ('yangqllovemysql',null),right('yangqllovemysql',9);
+---------------------------+-------------------------------+----------------------------+
| left('yangqllovemysql',6) | left ('yangqllovemysql',null) | right('yangqllovemysql',9) |
| yangql | | lovemysql |
--字元填充函數 lpad('yangql',11,'mysql')
mysql> select lpad('yangql',11,'mysql') ,rpad('mysql', 11,'yangql');
+---------------------------+----------------------------+
| lpad('yangql',11,'mysql') | rpad('mysql', 11,'yangql') |
| mysqlyangql | mysqlyangql |
--去掉str 左邊的空格!
mysql> select ltrim(' |yangqlmysql') ,rtrim('yangqlmysql| ');
+------------------------+--------------------------+
| ltrim(' |yangqlmysql') | rtrim('yangqlmysql| ') |
| |yangqlmysql | yangqlmysql| |
--repeat(str,n)重複 str n 次!
mysql> select repeat('yangql ',2);
+---------------------+
| repeat('yangql ',2) |
| yangql yangql |
--比較字元串的大小
mysql> select strcmp('a','b') ,strcmp('a','a'),strcmp('c','b');
+-----------------+-----------------+-----------------+
| strcmp('a','b') | strcmp('a','a') | strcmp('c','b') |
| -1 | 0 | 1 |
--substring(str,x,y)傳回從字元串str x 位置起y個字元長度的字元串!
mysql> select substring('yangql mysql',1,6) ,substring('yangql mysql',7,12);
+-------------------------------+--------------------------------+
| substring('yangql mysql',1,6) | substring('yangql mysql',7,12) |
| yangql | mysql |
--去掉 str 兩邊的空格,如果中間有空格,則不去掉!
mysql> select trim(' | yangql mysql |');
+---------------------------+
| trim(' | yangql mysql |') |
| | yangql mysql | |
--數值函數
--abs(x) 傳回x的絕對值。
mysql> select abs(-0.5),abs(0.5);
+-----------+----------+
| abs(-0.5) | abs(0.5) |
| 0.5 | 0.5 |
--ceil(x) :傳回大于x的最小整數。
mysql> select ceil(-0.5),ceil(0.5);
+------------+-----------+
| ceil(-0.5) | ceil(0.5) |
| 0 | 1 |
--floor(x):傳回小于x的最大整數,和ceil 相反!
mysql> select floor(-0.5),floor(0.5);
+-------------+------------+
| floor(-0.5) | floor(0.5) |
| -1 | 0 |
--随機值函數:傳回0--1 之間的數值,不重複!
mysql> select rand(),rand();
+------------------+-------------------+
| rand() | rand() |
| 0.65191487563021 | 0.031441814295428 |
--利用ceil 和rand 可以産生指定範圍内的随機數
mysql> select ceil(100*rand()),ceil(100*rand());
+------------------+------------------+
| ceil(100*rand()) | ceil(100*rand()) |
| 21 | 92 |
--round(x,y):傳回x的四舍五入的有y位 小數的值!
mysql> select round(1.25),round(1.25,1),round(1.25,2);
+-------------+---------------+---------------+
| round(1.25) | round(1.25,1) | round(1.25,2) |
| 1 | 1.3 | 1.25 |