赞
踩
- 语法:create table表名
-
- create table user(id int(10) primary key,username char(20) not null,passwd char(20) not null,email char(30) default "123456@qq.com");
- 语法:drop table表名
-
- 例:drop table student;
- 添加列:add,alter table user add列名int(12);
-
- 例:alter table user add phone int(12);//在user表中添加phone列
例:alter table user change phone phone int(12) not null ;//把原来user表中的phone,修改类型
例:alter table user drop phone;
- 语法:insert into表名(列名,列之间用逗号隔 开)values(插入的值,插入的值就用逗号隔开)
-
- 例:
- insert into sc values(9,4,2,87);//默认表的所有字段按顺序进行插入数据
- insert into sc(id,stu_id) values(10,3);//根据制定的列的顺序插入值,其他为表设置的默认值
- insert ignore into sc(id,stu_id) values(10,3);//数据存在则跳过
-
- //插入多条语句,只有数据后面添加数据就行,数据用逗号隔开,需要多条,就写多条
- insert into sc(id,stu_id,course_id,score) values(12,4,1,46),(13,4,1,5),(14,1,2,3)
-
- create table 复制表的表名 as select * from 原表名
-
- 例:create table student_copy as select * from student;
- insert into 原表名 select * from 复制表
-
- 例:insert into student select * from student_copy;
- update 表名 set stu_id=5 where stu_id is null;
-
- 例:update sc set stu_id=2,course_id=3,score=4 where id=1;
-
- 批量更新,适用于两个表直接关联
- UPDATE car c SET c.car_model_id =( SELECT car_model_id FROM car_attribute attribute WHERE c.car_attribute_id = attribute.car_attribute_id )
- WHERE car_brand_id = 0 ;
- 语法:delete from 表名
- 语法 truncate table 表名 ; //清空表数据,不可恢复,速度快,不可加where条件
-
- 例:delete from ecs_goods where goods_id>=140; //满足条件的数据删除
-
- 例:
- select * from ecs_goods where is_delete=0;
- select * from ecs_goods where is_delete=(>,<,>=,<=,!=)0 and(or,not) class ='一班';查询满足条件的数据。
- 查询出goods_name包含18K的产品名称
- select goods_name from ecs_goods where goods_name like‘%18K%’;
-
- 查询出goods_name末字段为18K的产品名称。
- select goods_name from ecs_goods where goods_name like‘%18K’;
-
- 已知goods_name里有一数据为“金钻石”,但是想不想来全名,查询出goods_name“金钻石”的产品名称。
- select goods_name from ecs_goods where goods_name like‘金钻_’;
- # ^ 以什么开头
- select id,name,job_title from emp where name regexp "^a";
- # $ 以什么结尾
- select id,name,job_title from emp where name regexp "san$";
- # 匹配指定字符内容
- select id,name,job_title from emp where name regexp "i{1}";
- 例:哪些商品的数量被3整除,并且展示产品的信息。
- select goods_name, goods_number mod 3 from ecs_goods where(goods_number MOD 3)=0
- 例:查询出产品信息并且用降序排序
- select * from ecs_goods order by goods_id desc;
- 可以理解为统计的是每一行的数据,只要这一行中有数据,就可以将他统计进去,所以一般统计是用*号,表示统计所有列,也是所有行,这样就能保证所有数据被统计,如果只是统计某一列,假如这一列的其中一行为null,其他有值,那就无法被统计,也就可能会失去一条数据。
-
- 例:统计ecs_goods总数
- select count(*) from ecs_goods;
-
- 求某一列的最大,最小,平均,求和
- select max(click_count) from ecs_goods;
- select min(click_count) from ecs_goods;
- select avg(click_count) from ecs_goods;
- select sum(click_count) from ecs_goods;
- 同一列数据,有相同的数据就合并。
- 例:select distinct(is_delete) from ecs_goods;
- 例:select is_delete from ecs_goods group by is_delete;
- 表示is_delete这一列,数据相同的合并,数量不显示,但是可以增加条件来显示同样的数据有多少条,distinct就只是表示把重复的去掉而已。
-
- 统计删除与未删除的数量
- select count(*),is_delete from ecs_goods group by is_delete;
- select * from ecs_goods having is_delete=0;
- 等价于
- select * from ecs_goods where is_delete=0;
-
- 但是分组以后只能使用having,表示对group up的分组进行条件约束,having后面可以使用聚合函数,where则不可以。
- 例:求出未删除的产品热销产品与非热销产品的数量大于2的。
- select is_hot,count(*)from ecs_goods where is_delete=0 group by is_hot having count(*)>2;
- 例:请显示student表格的前5行。
- select * from student limit 0,5; //0表示从第一行开始数,5表示一共数5行。
-
- 例:请显示student表格的5-10行。
- select * from student limit 4,10;
-
- 例:当第一个数字大于第二个数字时,表示从第20个值开始展示10条。
- select * from student limit 20,10;
-
- 例:表示从1条开始,展示10条。
- select * from student limit 10 offset 1;
-
- 例:删除前100条。
- delete from student order_by id desc limit 10;
- -- 方式一,先查询放到变量,再使用变量
- SELECT @driver_id:=`driver_id` FROM driver where phone=13728542050 ;
- SELECT* FROM driver where `driver_id`=@driver_id;
-
- -- 方式二,直接设置
- SET @driver_id='13728542050';
- SELECT* FROM driver where `driver_id`=@driver_id;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。