当前位置:   article > 正文

mysql基础_mysql insert select 逗号分隔 数组

mysql insert select 逗号分隔 数组

创建数据表

  1. 语法:create table表名
  2. 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");

删除表

  1. 语法:drop table表名
  2. 例:drop table student;

增加列 add

  1. 添加列:addalter table user add列名int(12);
  2. 例:alter table user add phone int(12);//user表中添加phone列

修改列的类型或约束:change。

例:alter table user change phone phone int(12) not null ;//把原来user表中的phone,修改类型

删除列:drop

例:alter table user drop phone;

插入语句

  1. 语法:insert into表名(列名,列之间用逗号隔 开)values(插入的值,插入的值就用逗号隔开)
  2. 例:
  3. insert into sc values(9,4,2,87);//默认表的所有字段按顺序进行插入数据
  4. insert into sc(id,stu_id) values(10,3);//根据制定的列的顺序插入值,其他为表设置的默认值
  5. insert ignore into sc(id,stu_id) values(10,3);//数据存在则跳过
  6. //插入多条语句,只有数据后面添加数据就行,数据用逗号隔开,需要多条,就写多条
  7. insert into sc(id,stu_id,course_id,score) values(12,4,1,46),(13,4,1,5),(14,1,2,3)

复制表

  1. create table 复制表的表名 as select * from 原表名
  2. 例:create table student_copy as select * from student;

还原表中数据

  1. insert into 原表名 select * from 复制表
  2. 例:insert into student select * from student_copy;

修改数据

  1. update 表名 set stu_id=5 where stu_id is null;
  2. 例:update sc set stu_id=2,course_id=3,score=4 where id=1;
  3. 批量更新,适用于两个表直接关联
  4. 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 )
  5. WHERE car_brand_id = 0 ;

删除数据

  1. 语法:delete from 表名
  2. 语法 truncate table 表名 ; //清空表数据,不可恢复,速度快,不可加where条件
  3. 例:delete from ecs_goods where goods_id>=140; //满足条件的数据删除

条件语句where

  1. 例:
  2. select * from ecs_goods where is_delete=0;
  3. select * from ecs_goods where is_delete=>,<,>=,<=,!=0 and(ornot) class ='一班';查询满足条件的数据。

模糊查询like,%匹配所有 ,_匹配单个字符

  1. 查询出goods_name包含18K的产品名称
  2. select goods_name from ecs_goods where goods_name like%18K%’;
  3. 查询出goods_name末字段为18K的产品名称。
  4. select goods_name from ecs_goods where goods_name like%18K’;
  5. 已知goods_name里有一数据为“金钻石”,但是想不想来全名,查询出goods_name“金钻石”的产品名称。
  6. select goods_name from ecs_goods where goods_name like‘金钻_’;

正则查询

  1. # ^ 以什么开头
  2. select id,name,job_title from emp where name regexp "^a";
  3. # $ 以什么结尾
  4. select id,name,job_title from emp where name regexp "san$";
  5. # 匹配指定字符内容
  6. select id,name,job_title from emp where name regexp "i{1}";

运算符:+,-*,/,mod

  1. 例:哪些商品的数量被3整除,并且展示产品的信息。
  2. select goods_name, goods_number mod 3 from ecs_goods where(goods_number MOD 3)=0

排序order by,默认升序,desc为降序

  1. 例:查询出产品信息并且用降序排序
  2. select * from ecs_goods order by goods_id desc;

count统计总数

  1. 可以理解为统计的是每一行的数据,只要这一行中有数据,就可以将他统计进去,所以一般统计是用*号,表示统计所有列,也是所有行,这样就能保证所有数据被统计,如果只是统计某一列,假如这一列的其中一行为null,其他有值,那就无法被统计,也就可能会失去一条数据。
  2. 例:统计ecs_goods总数
  3. select count(*) from ecs_goods;

最大值,最小值,平均值,求和

  1. 求某一列的最大,最小,平均,求和
  2. select max(click_count) from ecs_goods;
  3. select min(click_count) from ecs_goods;
  4. select avg(click_count) from ecs_goods;
  5. select sum(click_count) from ecs_goods;

distinct 去重

  1. 同一列数据,有相同的数据就合并。
  2. 例:select distinct(is_delete) from ecs_goods;

 分组group by,有点类似于去重

  1. 例:select is_delete from ecs_goods group by is_delete;
  2. 表示is_delete这一列,数据相同的合并,数量不显示,但是可以增加条件来显示同样的数据有多少条,distinct就只是表示把重复的去掉而已。
  3. 统计删除与未删除的数量
  4. select count(*),is_delete from ecs_goods group by is_delete;

 条件语句 having

  1. select * from ecs_goods having is_delete=0;
  2. 等价于
  3. select * from ecs_goods where is_delete=0;
  4. 但是分组以后只能使用having,表示对group up的分组进行条件约束,having后面可以使用聚合函数,where则不可以。
  5. 例:求出未删除的产品热销产品与非热销产品的数量大于2的。
  6. select is_hot,count(*)from ecs_goods where is_delete=0 group by is_hot having count(*)>2;

limit分页,第一行是从0开始数

  1. 例:请显示student表格的前5行。
  2. select * from student limit 0,5; //0表示从第一行开始数,5表示一共数5行。
  3. 例:请显示student表格的5-10行。
  4. select * from student limit 4,10;
  5. 例:当第一个数字大于第二个数字时,表示从第20个值开始展示10条。
  6. select * from student limit 20,10;
  7. 例:表示从1条开始,展示10条。
  8. select * from student limit 10 offset 1;
  9. 例:删除前100条。
  10. delete from student order_by id desc limit 10;

设置变量

  1. -- 方式一,先查询放到变量,再使用变量
  2. SELECT @driver_id:=`driver_id` FROM driver where phone=13728542050 ;
  3. SELECT* FROM driver where `driver_id`=@driver_id;
  4. -- 方式二,直接设置
  5. SET @driver_id='13728542050';
  6. SELECT* FROM driver where `driver_id`=@driver_id;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/268483
推荐阅读
相关标签
  

闽ICP备14008679号