SQL常用语句大全
1.插入数据
insert into 表名(列1,列2,列3)values(值1,值2,值3);
insert into product(name,price,pic_path) values('jack',25,'updown');
2.更新数据
update 表名 set 列名1=值1,列名2=值2 where 条件;
update product set name="jack",price=35 where id=2;
3.删除数据库
delete from 表名[where 条件]
delete from product where id=2;
4.查询所有数据库内容
select * from 表名;
select * from product;
5.查询部分列
select 列1,列2 from 表名;
select id,name from product;
6.条件查询
#比较 =,>,<,>=,<=,!=
select * from 表名 where列名=值;
select * from product where id=2;
#and与
select * from 表名 where 条件1 and 条件2 and 条件3;
select * from product where id=2 and name='Nike';
#or 或
select * from 表名 where 条件1 or 条件2 or 条件3;
select * from product where name='Nike' or id=2;
#not 非
select * from 表名 where not 条件1;
select * from product where not name='Nike';
#in枚举
select * from 表名where 列名 in(值1,值2,值3);
select * from product where id in(2,3,4,10);
select * from product where id not in(2,3,4);
#like模糊查询
select * from 表名where列名 like '%值%';
select * from product where name like '%LI%';
#between....and 范围查询
select * from 表名 where 列名 between 值1 and 值2;
select * from product where created between '2010-10-10' and '2011-10-10';
#limit行数查询
select * from 表名 limit n1,n2;(n1:从第几行开始,从0开始算;n2:要显示几行)
select * from product limit 3,4;
7.查询排序
select * from 表名 order by列表排序方式。
#排序方式:asc(升序),desc(降序);
select * from product order by created desc;
8.聚合函数
#count 总记录数
select count(列名) from student;
select count(id) from student;
#sum 总共
select sum(列名) from student;
select sum(age) from student;
#avg 平均值
select avg(列名)from student;
select avg(age) from student;
#max 最大值
select max(列名)from student;
select max(age) from student;
#min 最小值
select min(列名) from 表名;
select min(age) from student;
9.子查询
select name from student where age<(select avg(age) from student);
select * from product where id in(select id from order);