MySQL2 数据增删改查 SQL总结
数据操作
增 insert into
insert into in python values(1, "pycharm");
往python 数据表中添加一条数据, 也可以写成这样:
insert into python(name) values("aaa");
由于id是自动递增的,所有不用写id值也可以, 如果数据表中的字段是有默认值得,可以为空的,那么在添加的时候不添加也可以
添加多条数据:
insert into python(name) values("bb"),("cc"),("dd");
在values后面的括号以,隔开,这样就可以做到在一条SQL语句中插入多条内容了
删 delete
delete from python where id = 1;
删除ip等于1的记录, where 后面是删除数据的条件, 这个是物理删除,在日常开发中有许多数据是不能删除的,当又想做到删除效果,让用户觉得已经删除了该数据,这时候就可以一逻辑删除来操作了
先条件一个is_del 字段,用来标记是否是删除
alter table python add is_del tinyint default 0;
然后通过update来效果is_del 的值,做到逻辑删除效果
update python set is_del = 1 where id = 1;
这样就可以标记id为1 的值已经被逻辑删除了
查找数据的时候写过滤条件记得加上 where is_del = 0, 可以过滤掉被逻辑删除的数据
改 update
逻辑删除其实就是一个修改操作, 通过修改记录中的值来实现逻辑删除效果,如:
update python set name="666" where id = 1;
通过update 可以将id为1的数据记录的name改为666, 修改操作一般都带着 条件,不然一下子就改了这个数据表了
查 select
select * from python;
查找python数据表中所有的数据。
条件查找, 如 查找id为1的数据记录:
select * from python where id= 1;
SQL比较运算符:
等于:=
大于:>
大于等于:> =
小于:<
小于等于:<=
不等于:!=或<>
如:
mysql> select * from python where id > 1;
+----+------+--------+
| id | name | is_del |
+----+------+--------+
| 2 | bb | 0 |
| 3 | cc | 0 |
| 4 | dd | 0 |
+----+------+--------+
3 rows in set (0.00 sec)
mysql> select * from python where id >= 1;
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
| 2 | bb | 0 |
| 3 | cc | 0 |
| 4 | dd | 0 |
+----+---------+--------+
4 rows in set (0.00 sec)
可以通过比较运算符进行条件查询
逻辑运算符: and or not
如:
mysql> select * from python where id >= 1 and id <= 5;
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
| 2 | bb | 0 |
| 3 | cc | 0 |
| 4 | dd | 0 |
+----+---------+--------+
4 rows in set (0.00 sec)
mysql> select * from python where id >= 1 or name = 'pycharm';
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
| 2 | bb | 0 |
| 3 | cc | 0 |
| 4 | dd | 0 |
+----+---------+--------+
4 rows in set (0.00 sec)
mysql> select * from python where id not in(1,2,3);
+----+------+--------+
| id | name | is_del |
+----+------+--------+
| 4 | dd | 0 |
+----+------+--------+
1 row in set (0.10 sec)
连续查找, 可以使用 and, 也可以使用 between :
and
mysql> select * from python where id > 1 and id <= 10;
+----+------+--------+
| id | name | is_del |
+----+------+--------+
| 2 | bb | 0 |
| 3 | cc | 0 |
| 4 | dd | 0 |
+----+------+--------+
3 rows in set (0.00 sec)
between
mysql> select * from python where id between 1 and 2;
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
| 2 | bb | 0 |
+----+---------+--------+
2 rows in set (0.00 sec)
非连续查询 () :
mysql> select * from python where id in (1, 3, 5);
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
| 3 | cc | 0 |
+----+---------+--------+
2 rows in set (0.00 sec)
模糊查询 like, 模糊查询是一种很常见的查询,如生活中的关键字查询,在SQL语句中有like来表示, 语法上有 %, _ ,其中%表示的是任意多个或没有的字符, 而_则代表必须要有一位, 如:
mysql> select * from python where name like '%p%';
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
+----+---------+--------+
1 row in set (0.00 sec)
可以看到,在SQL查询中 p 前面没有数据的记录也被查找出来了,
mysql> select * from python where name like '_p%';
Empty set (0.00 sec)
mysql> select * from python where name like '_y%';
+----+---------+--------+
| id | name | is_del |
+----+---------+--------+
| 1 | pycharm | 1 |
+----+---------+--------+
1 row in set (0.00 sec)
可以看到,_ 必须占一位,如果 p 前面没有数据了就不会被查询出来
null 判断, 在数据表中如果一个字段设置为可以为null,那么,将要通过 is null 查找,也可以是 is not null 如:
select * from python where name is null;
select * from python where name is not null;
给字段请别名:
select name as nm from python where id= 1;
给数据表起别名, 一般在多表操作中使用到,:
select name as nm from python where id= 1;
select py.name as nm from python as py where id= 1;