mysql> SELECT 1+1,1-1,2*4,3/8;
+-----+-----+-----+--------+
| 1+1 | 1-1 | 2*4 | 3/8 |
+-----+-----+-----+--------+
| 2 | 0 | 8 | 0.3750 |
+-----+-----+-----+--------+
#数字和字符串相加会有警告
mysql> SELECT 1+'3MAIZI';
+------------+
| 1+'3MAIZI' |
+------------+
| 4 |
+------------+
1 row in set, 1 warning (0.03 sec)
mysql> SHOW WARNINGS;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '3MAIZI' |
+---------+------+--------------------------------------------+
mysql> SELECT 3%8;
+------+
| 3%8 |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
#也可以表达为:SELECT MOD(3,8);
注意:除法用 DIV()的形式不行,必须: X1 DIV X2
mysql> SELECT 3 MOD 8;
+---------+
| 3 MOD 8 |
+---------+
| 3 |
+---------+
1 row in set (0.00 sec)
mysql> SELECT 1+NULL;
+--------+
| 1+NULL |
+--------+
| NULL |
+--------+
1 row in set (0.00 sec)
mysql> SELECT 1=1,1='1',1=2;
+-----+-------+-----+
| 1=1 | 1='1' | 1=2 |
+-----+-------+-----+
| 1 | 1 | 0 |
+-----+-------+-----+
1 row in set (0.00 sec)
调出学生表
mysql> SELECT * FROM student;
+----+----------+-------+
| id | username | score |
+----+----------+-------+
| 1 | king | 95 |
| 2 | king1 | 35 |
| 3 | king2 | 45 |
| 4 | king3 | 55 |
| 5 | king4 | 65 |
| 6 | king5 | 75 |
| 7 | king6 | 80 |
| 8 | king7 | 90 |
| 9 | king8 | 25 |
+----+----------+-------+
mysql> SELECT username,username='king' FROM student;
+----------+-----------------+
| username | username='king' |
+----------+-----------------+
| king | 1 |
| king1 | 0 |
| king2 | 0 |
| king3 | 0 |
| king4 | 0 |
| king5 | 0 |
| king6 | 0 |
| king7 | 0 |
| king8 | 0 |
+----------+-----------------+
SELECT username,username!='king' FROM student;
创建一个新表,插入一条值为NULL的数据
CREATE TABLE abc (id TINYINT);
INSERT abc VALUE(NULL);
mysql> SELECT * FROM abc;
+------+
| id |
+------+
| NULL |
+------+
mysql> SELECT id<=>null FROM abc;
+-----------+
| id<=>null |
+-----------+
| 1 |
+-----------+
mysql> SELECT id=null FROM abc;
+---------+
| id=null |
+---------+
| NULL |
+---------+
mysql> SELECT id IS null FROM abc;
+------------+
| id IS null |
+------------+
| 1 |
+------------+
SELECT id,username,age,age BETWEEN 10 AND 30 FROM cms_user;
包括10和30
mysql> SELECT 's' LIKE '_';
+--------------+
| 's' LIKE '_' |
+--------------+
| 1 |
+--------------+
SELECT id,username,username LIKE '__' FROM cms_user;
结果,只有张三匹配结果为1
SELECT id,username,username REGEXP '^t' FROM cms_user;
只有t/T开头的匹配结果为1
mysql> SELECT 2&&2,2&&0,2&&NULL,1||1,1||0,1||NULL,0||NULL;
+------+------+---------+------+------+---------+---------+
| 2&&2 | 2&&0 | 2&&NULL | 1||1 | 1||0 | 1||NULL | 0||NULL |
+------+------+---------+------+------+---------+---------+
| 1 | 0 | NULL | 1 | 1 | 1 | NULL |
+------+------+---------+------+------+---------+---------+
注意有NULL参与
的情况
mysql> SELECT !1,!0,!NULL;
+----+----+-------+
| !1 | !0 | !NULL |
+----+----+-------+
| 0 | 1 | NULL |
+----+----+-------+
注意有NULL的情况
不同为真,相同为假
mysql> SELECT 1 XOR 0,0 XOR 1 ,1 XOR 1,0 XOR 0;
+---------+---------+---------+---------+
| 1 XOR 0 | 0 XOR 1 | 1 XOR 1 | 0 XOR 0 |
+---------+---------+---------+---------+
| 1 | 1 | 0 | 0 |
+---------+---------+---------+---------+