mysql delete操作
语法:delete from 表名 where.
mysql> select * from user;
+----+--------+---------------------+
| id | name | birthday |
+----+--------+---------------------+
| 1 | lisi | 2000-08-09 00:00:00 |
| 2 | saluya | 2010-01-11 00:00:00 |
| 3 | lucy | 1993-07-26 00:00:00 |
+----+--------+---------------------+
3 rows in set (0.00 sec)
mysql> delete from user where id = 3;
Query OK, 1 row affected (0.01 sec)
mysql> select * from user;
+----+--------+---------------------+
| id | name | birthday |
+----+--------+---------------------+
| 1 | lisi | 2000-08-09 00:00:00 |
| 2 | saluya | 2010-01-11 00:00:00 |
+----+--------+---------------------+
2 rows in set (0.00 sec)
注意:如果后面不跟where条件过滤,所有的行都会收到影响。
mysql select操作
语法:select 列1,列2.... from 表名 where expr.
#查询所有的数据,数据量过大会使表崩。
mysql> select * from user;
+----+--------+---------------------+
| id | name | birthday |
+----+--------+---------------------+
| 1 | lisi | 2000-08-09 00:00:00 |
| 2 | saluya | 2010-01-11 00:00:00 |
+----+--------+---------------------+
2 rows in set (0.00 sec)
#增加where条件过滤,只查id=2的行。
mysql> select * from user where id=2;
+----+--------+---------------------+
| id | name | birthday |
+----+--------+---------------------+
| 2 | saluya | 2010-01-11 00:00:00 |
+----+--------+---------------------+
1 row in set (0.00 sec)
#查询id>=2的行。
mysql> select * from user where id>=2;
+----+--------+---------------------+
| id | name | birthday |
+----+--------+---------------------+
| 2 | saluya | 2010-01-11 00:00:00 |
| 3 | lili | 2009-09-01 00:00:00 |
+----+--------+---------------------+
2 rows in set (0.00 sec)
#固定查询只查看哪几列。
mysql> select id,name from user where id>=2;
+----+--------+
| id | name |
+----+--------+
| 2 | saluya |
| 3 | lili |
+----+--------+
2 rows in set (0.00 sec)
select查询模型:
列是变量,where是表达式,值为真假.
#注意:1为true,0为fales,直接写ture也是可以的。
mysql> select id,name from user where 1;
+----+--------+
| id | name |
+----+--------+
| 1 | lisi |
| 2 | saluya |
| 3 | lili |
+----+--------+
3 rows in set (0.00 sec)
#注意:变量为假一行也打印不出来.
mysql> select id,name from user where 0;
Empty set (0.00 sec)
#注意:where条件写布尔型也是可以匹配到的.
mysql> select id,name from user where true;
+----+--------+
| id | name |
+----+--------+
| 1 | lisi |
| 2 | saluya |
| 3 | lili |
+----+--------+
3 rows in set (0.00 sec)
比较运算符:
等于 =
不等于 != 或者 <>
小于 <
小于等于 <=
大于(等于) >(=)
大于等于 >=
在某集合内 in (等于in(a,b,...,n)中的任意一个值都行
在某个范围内 between (在between a and b 之间的值都可以,允许等于边界值)
逻辑运算符:
逻辑非 NOT 或 !
逻辑或 OR 或 ||
逻辑与 AND 或 &&
模糊查询:
像 like
通配符:
通配任意字符 % (like(诺基亚%))
通配单个字符 _ (like(诺基亚N__))
#变量可以做‘+’,‘-’运算,在mysql数据库种也一样,但是要针对可以做运算的列.
mysql> select id+1 from user where 1;
+------+
| id+1 |
+------+
| 2 |
| 3 |
| 4 |
+------+
错误案例:
如果不能做加减运算的列,结果如下:
mysql> select id,name+1 from user where 1;
+----+--------+
| id | name+1 |
+----+--------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
+----+--------+
3 rows in set, 3 warnings (0.00 sec)
#mysql列与列之间可以做“+”,“-”法运算,称之为广义投影.
mysql> select * from student;
+-----+--------+------+------+
| SNO | SNAME | AGE | SEX |
+-----+--------+------+------+
| 1 | 李强 | 23 | 男 |
| 2 | 刘丽 | 22 | 女 |
| 5 | 张友 | 22 | 男 |
+-----+--------+------+------+
3 rows in set (0.00 sec)
mysql> select SNAME,SEX,AGE-SNO from student;
+--------+------+---------+
| SNAME | SEX | AGE-SNO |
+--------+------+---------+
| 李强 | 男 | 22 |
| 刘丽 | 女 | 20 |
| 张友 | 男 | 17 |
+--------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from student;
+-----+--------+------+------+
| SNO | SNAME | AGE | SEX |
+-----+--------+------+------+
| 1 | 李强 | 23 | 男 |
| 2 | 刘丽 | 22 | 女 |
| 5 | 张友 | 22 | 男 |
+-----+--------+------+------+
3 rows in set (0.00 sec)
#查找student表中,所有年龄不等于!=22和<>22的学生名字,两种写法都行.
mysql> select SNAME from student where AGE != 22;
+--------+
| SNAME |
+--------+
| 李强 |
+--------+
1 row in set (0.00 sec)
mysql> select SNAME from student where AGE <> 22;
+--------+
| SNAME |
+--------+
| 李强 |
+--------+
1 row in set (0.00 sec)
#查询年级大于22岁的学生名字.
mysql> select SNAME from student where AGE > 22;
+--------+
| SNAME |
+--------+
| 李强 |
+--------+
1 row in set (0.00 sec)
#查询年龄等于22或者等于23的学生名字,"or"用法.
mysql> select SNAME,AGE from student where AGE=23 or AGE= 22;
+--------+------+
| SNAME | AGE |
+--------+------+
| 李强 | 23 |
| 刘丽 | 22 |
| 张友 | 22 |
+--------+------+
3 rows in set (0.00 sec)
#查询年龄在22,23的学生名字,"in"用法,在某几个值之间.
mysql> select SNAME,AGE from student where AGE in (22,23);
+--------+------+
| SNAME | AGE |
+--------+------+
| 李强 | 23 |
| 刘丽 | 22 |
| 张友 | 22 |
+--------+------+
3 rows in set (0.00 sec)
#查询年龄在22,23的学生名字,"between"用法,取值范围之间.
mysql> select SNAME,AGE from student where AGE between 22 and 23;
+--------+------+
| SNAME | AGE |
+--------+------+
| 李强 | 23 |
| 刘丽 | 22 |
| 张友 | 22 |
+--------+------+
3 rows in set (0.00 sec)
mysql> select SNAME,AGE from student where AGE between 0 and 23;
+--------+------+
| SNAME | AGE |
+--------+------+
| 李强 | 23 |
| 刘丽 | 22 |
| 张友 | 22 |
+--------+------+
3 rows in set (0.00 sec)
#找出年龄不等于23也不等于23的学生,“not in”用法.
mysql> select SNAME,AGE from student where AGE != 22 and AGE !=23;
Empty set (0.00 sec)
mysql> select SNAME,AGE from student where AGE not in (22,23);
Empty set (0.00 sec)
#取出性别为“男”,年级大于0且小于等于22岁并且学号为5的学生名字.
mysql> select * from student;
+-----+--------+------+------+
| SNO | SNAME | AGE | SEX |
+-----+--------+------+------+
| 1 | 李强 | 23 | 男 |
| 2 | 刘丽 | 22 | 女 |
| 5 | 张友 | 22 | 男 |
+-----+--------+------+------+
3 rows in set (0.00 sec)
mysql> select SNAME from student where SEX="男" and (AGE > 0 and AGE <=22) and SNO = 5;
+--------+
| SNAME |
+--------+
| 张友 |
+--------+
1 row in set (0.00 sec)
注意:多个条件直接使用“()”括起来,"and"的优先级大于"or",条件过多时会发生歧义。
#mysql种like的用法,查找SNAME像“%强”的学生,“%”匹配任意字符.
mysql> select SNAME from student where SNAME like "%强";
+--------+
| SNAME |
+--------+
| 李强 |
+--------+
1 row in set (0.00 sec)
#mysql种“_”匹配单一字符.
mysql> select SNAME from student where SNAME like "_强";
+--------+
| SNAME |
+--------+
| 李强 |
+--------+
1 row in set (0.00 sec)
#将student表中的名字像“_强”的改为“换换”.
mysql> update student set SNAME = "换换" where SNAME like "_强";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+-----+--------+------+------+
| SNO | SNAME | AGE | SEX |
+-----+--------+------+------+
| 1 | 换换 | 23 | 男 |
| 2 | 刘丽 | 22 | 女 |
| 5 | 张友 | 22 | 男 |
+-----+--------+------+------+
3 rows in set (0.00 sec)