数据库表内容的操作
插入数据:
insert into 表名(列名1,列名2,列名4) values(值1,值2,值4);
插入多行:
insert into 表名(列名1,列名2,列名4) values(值1,值2,值4),(值1,值2,值4),(值1,值2,值4);
如果是所有的列都需要写入数据,则表名后不需要添加列名,除了列名有约束;
查看所有的数据:select * from student;
修改ini可以修改编码错误 的情况;
删除数据
delete from student where id=19;
delete from student;删除所有
delete 属于dml,一条一条的删除数据;
truncate 属于 ddl,先删除表再重建表
哪个的效率高,是根据数据来判断的;
更新表
update 表名 set 列名=列的值,列名2=列的值 where 条件
update student set name=“yang” ,sex=2 where id=1;
查询操作
select distinct * from student where nam=10;
distinct 去除重复的数据
select p.name from product as p;查询的简单写法;
select name as 名字 from product;as 可以省略不写
select price*3 as 价格 from product;
where price > = < != <>这个是不等于,标准SQL用法
where price =1 and price=3;
where price between 10 and 39;
where price <100 or price >200;
模糊查询like
_ 代表一个字符;
%代表多个字符;
where pname like “%小米%”;
in 在摸个范围查找值
where price in(1,2,3);
排序 order by
asc:升序
desc降序
select * from student order by price desc;
select * from student where name=“yang” order by id desc;
聚合函数;(where 后面不能够接聚合函数)
但是where price >(select avg(price) from product);
sun avg count 统计数量 max min
select sum(price) from product
分组:根据某个字段分组,分组后统计个数
group by
select * ,count(*) from studnet group by id;;
根据id分组,分组统计每组商品的平均价格,并且平均价格大于69
select * ,avg(price) from sudnt group by id having avg(price)>69;
having关键字可以接聚合函数,出现在分组之后
where 关键字出现在分组之前