MYSQL入門學習之六:MYSQL的運算符 相關鏈接: MYSQL入門學習之一:基本操作 http://www.BkJia.com/database/201212/173868.html MYSQL入門學習之二:使用正則表達式搜索 http://www.BkJia.com/database/201212/173869.html MYSQL入門學習之三:全文本搜索 http://www.BkJia.com/database/201212/173873.html MYSQL入門學習之四:MYSQL的數據類型 http://www.BkJia.com/database/201212/175536.html MYSQL入門學習之五:MYSQL的字符集 http://www.BkJia.com/database/201212/175541.html 一、算術運算符 1、加 www.2cto.com mysql> select 1+2; +-----+ | 1+2 | +-----+ | 3 | +-----+ 2、減 mysql> select 1-2; +-----+ | 1-2 | +-----+ | -1 | +-----+ 3、乘 mysql> select 2*3; +-----+ | 2*3 | +-----+ | 6 | +-----+ 4、除 mysql> select 2/3; +--------+ | 2/3 | +--------+ | 0.6667 | +--------+ 5、商 www.2cto.com mysql> select 10 DIV 4; +----------+ | 10 DIV 4 | +----------+ | 2 | +----------+ 6、取余 mysql> select 10 MOD 4; +----------+ | 10 MOD 4 | +----------+ | 2 | +----------+ 二、比較運算符 1、等於 mysql> select 2=3; +-----+ | 2=3 | +-----+ | 0 | +-----+ mysql> select NULL = NULL; +-------------+ | NULL = NULL | +-------------+ | NULL | +-------------+ 2、不等於 mysql> select 2<>3; +------+ | 2<>3 | +------+ | 1 | +------+ 3、安全等於 與“=”的區別在於當兩個操作碼均為NULL時,其所得值為1而不為NULL,而當一個操作碼為NULL時,其所得值為0而不為NULL。 mysql> select 2<=>3; +-------+ | 2<=>3 | +-------+ | 0 | +-------+ mysql> select null=null; +-----------+ | null=null | +-----------+ | NULL | +-----------+ mysql> select null<=>null; +-------------+ | null<=>null | +-------------+ | 1 | +-------------+ 4、小於 mysql> select 2<3; +-----+ | 2<3 | +-----+ | 1 | +-----+ 5、小於等於 mysql> select 2<=3; +------+ | 2<=3 | +------+ | 1 | +------+ 6、大於 mysql> select 2>3; +-----+ | 2>3 | +-----+ | 0 | +-----+ 7、大於等於 mysql> select 2>=3; +------+ | 2>=3 | +------+ | 0 | +------+ 8、BETWEEN mysql> select 5 between 1 and 10; +--------------------+ | 5 between 1 and 10 | +--------------------+ | 1 | +--------------------+ 9、IN mysql> select 5 in (1,2,3,4,5); +------------------+ | 5 in (1,2,3,4,5) | +------------------+ | 1 | +------------------+ 10、NOT IN mysql> select 5 not in (1,2,3,4,5); +----------------------+ | 5 not in (1,2,3,4,5) | +----------------------+ | 0 | +----------------------+ 11、IS NULL mysql> select null is NULL; +--------------+ | null is NULL | +--------------+ | 1 | +--------------+ mysql> select 'a' is NULL; +-------------+ | 'a' is NULL | +-------------+ | 0 | +-------------+ 12、IS NOT NULL mysql> select null IS NOT NULL; +------------------+ | null IS NOT NULL | +------------------+ | 0 | +------------------+ mysql> select 'a' IS NOT NULL; +-----------------+ | 'a' IS NOT NULL | +-----------------+ | 1 | +-----------------+ 13、LIKE mysql> select '12345' like '12%'; +--------------------+ | '12345' like '12%' | +--------------------+ | 1 | +--------------------+ mysql> select '12345' like '12_'; +--------------------+ | '12345' like '12_' | +--------------------+ | 0 | +--------------------+ 14、REGEXP mysql> select 'beijing' REGEXP 'jing'; +-------------------------+ | 'beijing' REGEXP 'jing' | +-------------------------+ | 1 | +-------------------------+ mysql> select 'beijing' REGEXP 'xi'; +-----------------------+ | 'beijing' REGEXP 'xi' | +-----------------------+ | 0 | +-----------------------+ 三、邏輯運算符 www.2cto.com 1、與 mysql> select 2 and 0; +---------+ | 2 and 0 | +---------+ | 0 | +---------+ mysql> select 2 and 1; +---------+ | 2 and 1 | +---------+ | 1 | +---------+ 2、或 mysql> select 2 or 0; +--------+ | 2 or 0 | +--------+ | 1 | +--------+ mysql> select 2 or 1; +--------+ | 2 or 1 | +--------+ | 1 | +--------+ mysql> select 0 or 0; +--------+ | 0 or 0 | +--------+ | 0 | +--------+ mysql> select 1 || 0; +--------+ | 1 || 0 | +--------+ | 1 | +--------+ 3、非 mysql> select not 1; +-------+ | not 1 | +-------+ | 0 | +-------+ mysql> select !0; +----+ | !0 | +----+ | 1 | +----+ 4、異或 www.2cto.com mysql> select 1 xor 1; +---------+ | 1 xor 1 | +---------+ | 0 | +---------+ mysql> select 0 xor 0; +---------+ | 0 xor 0 | +---------+ | 0 | +---------+ mysql> select 1 xor 0; +---------+ | 1 xor 0 | +---------+ | 1 | +---------+ mysql> select null or 1; +-----------+ | null or 1 | +-----------+ | 1 | +-----------+ mysql> select 1 ^ 0; +-------+ | 1 ^ 0 | +-------+ | 1 | +-------+ 四、位運算符 1、按位與 mysql> select 3&5; +-----+ | 3&5 | +-----+ | 1 | +-----+ 2、按位或 mysql> select 3|5; +-----+ | 3|5 | +-----+ | 7 | +-----+ 3、按位異或 mysql> select 3^5; +-----+ | 3^5 | +-----+ | 6 | +-----+ 4、按位取反 +----------------------+ | ~3 | +----------------------+ | 18446744073709551612 | +----------------------+ mysql> select ~18446744073709551612; +-----------------------+ | ~18446744073709551612 | +-----------------------+ | 3 | +-----------------------+ 5、按位右移 www.2cto.com mysql> select 3>>1; +------+ | 3>>1 | +------+ | 1 | +------+ 6、按位左移 mysql> select 3<<1; +------+ | 3<<1 | +------+ | 6 | +------+ 五、運算符優先級順序 最高優先級 := 1 ||, OR, XOR 2 &&, AND 3 BETWEEN, CASE, WHEN, THEN, ELSE 4 =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN 5 | 6 & 7 <<, >> 8 -, + 9 *, /, DIV, %, MOD 10 ^ 11 - (unary minus), ~ (unary bit inversion) 12 !, NOT 最低優先級 BINARY, COLLATE