在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、算数运算符MySQL支持的算术运算符:
例1: +,-,*,/,%, mysql> select 0.1+0.333,0.1-0.333,0.1*0.333,1/2,1%2; +-----------+-----------+-----------+--------+------+ | 0.1+0.333 | 0.1-0.333 | 0.1*0.333 | 1/2 | 1%2 | +-----------+-----------+-----------+--------+------+ | 0.433 | -0.233 | 0.0333 | 0.5000 | 1 | +-----------+-----------+-----------+--------+------+ 1 row in set (0.05 sec) 例2:除法运算中,如果除数为 mysql> select 1/0; +------+ | 1/0 | +------+ | NULL | +------+ 1 row in set (0.00 sec) 例3:模运算的另外一种形式 mysql> select 1%2,mod(1,2); +------+----------+ | 1%2 | mod(1,2) | +------+----------+ | 1 | 1 | +------+----------+ 1 row in set (0.00 sec) 2、比较运算符MySQL支持的比较运算符:
例1:“ mysql> select 1<>0,1<>1,null<>null; +------+------+------------+ | 1<>0 | 1<>1 | null<>null | +------+------+------------+ | 1 | 0 | NULL | +------+------+------------+ 1 row in set (0.00 sec) 例2: “ mysql> select 1<=>1,1<=>0,null<=>null; +-------+-------+-------------+ | 1<=>1 | 1<=>0 | null<=>null | +-------+-------+-------------+ | 1 | 0 | 1 | +-------+-------+-------------+ 1 row in set (0.02 sec) 例3: “ mysql> select 'a'<'b','A'<'b','bdf'<'c',1<2; +---------+---------+-----------+-----+ | 'a'<'b' | 'A'<'b' | 'bdf'<'c' | 1<2 | +---------+---------+-----------+-----+ | 1 | 1 | 1 | 1 | +---------+---------+-----------+-----+ 1 row in set (0.02 sec) 例4: mysql> select 10 between 10 and 20,9 between 10 and 20; +----------------------+---------------------+ | 10 between 10 and 20 | 9 between 10 and 20 | +----------------------+---------------------+ | 1 | 0 | +----------------------+---------------------+ 1 row in set (0.01 sec) 例5: mysql> select 'abcdeef' regexp 'ab','abcdef' regexp 'g','abcedf' regexp 'df'; +-----------------------+---------------------+----------------------+ | 'abcdeef' regexp 'ab' | 'abcdef' regexp 'g' | 'abcedf' regexp 'df' | +-----------------------+---------------------+----------------------+ | 1 | 0 | 1 | +-----------------------+---------------------+----------------------+ 1 row in set (0.01 sec) 其他的使用简单,直接上语法,示例就不写了
3、逻辑运算符MySQL中的逻辑运算符:
例1: ““ mysql> select not 0,!0,not 1,not null; +-------+----+-------+----------+ | not 0 | !0 | not 1 | not null | +-------+----+-------+----------+ | 1 | 1 | 0 | NULL | +-------+----+-------+----------+ 1 row in set (0.00 sec) 例2: “ mysql> select (1 and 1),(0 and 1),(3 and 1),(0 and null),(1 and null); +-----------+-----------+-----------+--------------+--------------+ | (1 and 1) | (0 and 1) | (3 and 1) | (0 and null) | (1 and null) | +-----------+-----------+-----------+--------------+--------------+ | 1 | 0 | 1 | 0 | NULL | +-----------+-----------+-----------+--------------+--------------+ 1 row in set (0.00 sec) mysql> select 1 and NULL and 0; +------------------+ | 1 and NULL and 0 | +------------------+ | 0 | +------------------+ 1 row in set (0.00 sec) mysql> select 1 and NULL and 3; +------------------+ | 1 and NULL and 3 | +------------------+ | NULL | +------------------+ 1 row in set (0.00 sec) 例3: “ mysql> select (1 or 0),(0 or 0),(1 or NULL),(0 or NULL),(NULL or NULL); +----------+----------+-------------+-------------+----------------+ | (1 or 0) | (0 or 0) | (1 or NULL) | (0 or NULL) | (NULL or NULL) | +----------+----------+-------------+-------------+----------------+ | 1 | 0 | 1 | NULL | NULL | +----------+----------+-------------+-------------+----------------+ 1 row in set (0.00 sec) 例4: mysql> select (0 xor 0),(1 xor 0),(1 xor 1),(1 xor null),(0 xor null),(null xor null); +-----------+-----------+-----------+--------------+--------------+-----------------+ | (0 xor 0) | (1 xor 0) | (1 xor 1) | (1 xor null) | (0 xor null) | (null xor null) | +-----------+-----------+-----------+--------------+--------------+-----------------+ | 0 | 1 | 0 | NULL | NULL | NULL | +-----------+-----------+-----------+--------------+--------------+-----------------+ 1 row in set (0.00 sec) 4、位运算符MySQL支持的位运算符:
例1: “位与”对多个操作数的二进制位作逻辑与操作。2&3,2的二进制数为10,3的二进制数为11,将此做与操作,结果还是10,转换为十进制结果就是2 mysql> select 2&3; +-----+ | 2&3 | +-----+ | 2 | +-----+ 1 row in set (0.01 sec) 例2: “位或”对多个操作数的二进制位作逻辑或操作。2&3,2的二进制数为10,3的二进制数为11,将此做与操作,结果就变成11,转换为十进制结果就是3 mysql> select 2|3; +-----+ | 2|3 | +-----+ | 3 | +-----+ 1 row in set (0.00 sec) 例3: “ mysql> select 2^3; +-----+ | 2^3 | +-----+ | 1 | +-----+ 1 row in set (0.01 sec) 例4: “位取反”对操作数的二进制位做NOT操作,这里的操作数只能是一位,解释:在mysql中常亮数字默认会以8字节表示,8个字节就是64位,而常量1的二进制 就是前面63个0,1个1,位去反后就是63个1,1个0,转换为二进制后就是 18446744073709551614, mysql> select ~1,~18446744073709551614 -> ; +----------------------+-----------------------+ | ~1 | ~18446744073709551614 | +----------------------+-----------------------+ | 18446744073709551614 | 1 | +----------------------+-----------------------+ 1 row in set (0.01 sec) mysql> select bin(18446744073709551614); +------------------------------------------------------------------+ | bin(18446744073709551614) | +------------------------------------------------------------------+ | 1111111111111111111111111111111111111111111111111111111111111110 | +------------------------------------------------------------------+ 1 row in set (0.03 sec) 例5: “位右移”对左操作数向右移动操作数指定的位数。例如100>>3,对100的二进制数0001100100右移动3位,0000001100,转换为二进制数就是12: mysql> select 100>>3; +--------+ | 100>>3 | +--------+ | 12 | +--------+ 1 row in set (0.00 sec) 例6: “位左移”对左操作数向左移动操作数指定的位数。例如100<<3,对100的二进制数0001100100000右移动3位,1100100000000,转换为二进制数就是800: mysql> select 100<<3; +--------+ | 100<<3 | +--------+ | 800 | +--------+ 1 row in set (0.00 sec) 5、运算符的优先级MySQL中的运算符优先级:
到此这篇关于MYSQL 运算符总结的文章就介绍到这了,更多相关MYSQL 运算符内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论