//单行数据+全列插入(值的类型和数量要和表相同)
insert into 表名 values (值,值,值...);
//单行数据+指定列插入
insert into 表名 ( , , ,) values
(值,值,值...),
(值,值,值...),
(值,值,值...),
...;
在插入时,要按照数据的类型和个数对应,没有插入的类型会赋值为NULL;
//全列查询
select * from 表名;
//指定列查询
select , , from 表名;
//查询字段为表达式
select , , , from 表名;
//去重
select distinct 列名 from 表名;
//别名
select 列名, 列名+列名 as 新列名 from 表名;
//排序:
//升序排列:用列名1或列名2的顺序排列;
select 列名1,列名2 from 表名 order by 列名1(2);
//降序排列:用列名1或列名2的顺序排列;
select 列名1,列名2 from 表名 order by 列名1(2) desc;
//多行排列:先按照列名1降序排列,在按照列名2升序排列;
select 列名1,列名2 from 表名 order by 列名1 desc,列名2;
//如果排列数据中,某一列有‘NULL’,则整个列都视为最小值;
//查询:
//条件查询:
select * from 表名 where 条件;
//例如:select * fom good where price > 20;
//and 和 or:
select 列名1,列名2,列名3 from 表名 where 条件1 and 条件2;
select 列名1,列名2,列名3 from 表名 where 条件1 or 条件2;
//例如:select name,price,quantity from good where price > 30 and quantity >20;
// select name,price,quantity from good where price > 30 or quantity >20;
//between and 和 in();
select 列名1,列名2,列名3 from 表名 where 列名 between 条件1 and 条件2;
select 列名1,列名2,列名3 from 表名 where 列名 in( , , , );
//例如:select name,price,quantity from good where price between 20 and 50;
// select name,price,quantity from good where price in(20,30,40,50);
//模糊查询:%代表任意个任意字符;_代表一个任意字符
select 列名 from 表名 where 列名 like ' %';
select 列名 from 表名 where 列名 like ' _';
//例如:select name from good where name like '毛%';
// select name from good where name like '毛_';
//null查询:
select 列名1,列名2 from 表名 where 列名 is (not) null;
//例如:select name,price from good where name is null;(where name is null);
// select name,price from good where name is not null;
//分页查询:limit限制返回结果的条数,offset确定第几条开始返回
select 列名1,... from 表名 limit n;(从第0条开始返回)
select 列名1,... from 表名 limit m,n;
select 列名1,... from 表名 limit m offset n;
//修改:是针对服务器的,更改是持续有效的
update 表名 set 列名 = 值(,列名 = 值...) where 条件;
//例如:update good set price = 99.99 where name = '筷子';
//删除:
delete from 表名 where 列名 = 值;
总结:
- 在排序查询时,NULL视为最小值,order by默认是升序排列,其后加desc是降序排列;
- 在mysql中,null和其他类型运算时,结果为null;
- 在模糊查询中,通配符%表示任意个任意字符,_表示一个任意字符;
- update修改是针对服务器的,修改后的结果持续且