mysql命令去重_MySQL 命令操作

Mysql进入与退出

进入数据库:mysql -uroot -p

退出: exit

库级操作语句

显示所有的表:show databases;

创建库: create database 库名; (重复创建会报错,可在库名前加上[if not exists])

删除库: drop database 库名;(如果不知道数据库是否存在,记得在库名前加[if exists])

进入数据库:use 库名;

表级操作语句

显示所有的表: show tables;

创建表: create table 表名;(重复创建会报错,可在表名前加上[if not exists])

显示创建表的信息: show create table 表名;

删除表:drop table 表名;

MysSQL表中数据的操作

插入数据(create) insert into values

指定字段插入: insert into 表名(表头)value(插入信息);

全字段插入:  insert into 表名value (插入整段信息);

多行插入: insert into 表名 value(插入信息),(插入信息),…;

查询数据 (read) select from where

指定字段查询: select 表头 from 表名;

2.全字段查询:select * from 表名;(select查询关键字,  * 代表所有 )

带条件查询:select 表头1 from 表名 where 表头2  条件;

例:select name from student where age = 19;

(查询学生表里年龄等于19的姓名)

select * from 表名 where 表头字段 条件;

例:select * from student where age = 19;

(查询学生表里等于19岁的全部字段)

修改数据(update)update set where

修改所有数据:update 表名 set 表头字段 = 条件;

修改多个数据:update 表名 set 表头字段 = 条件, 表头字段2 = 条件2;

修改满足条件的数据:update 表名 set 表头字段1 = 条件1  where 表头字段2 = 条件2;

例:update student set id = 5 where name =某某;

(把学生表姓名为某某的id改成5)

删除数据 (delete)delete from where

删除表中所有数据: delete from 表头字段;

删除表中满足条件的数据:delete from 表头字段 where 表头字段 = 条件;

例:delete from student where name = 某某;

(删除学生表里某某整行值)

MySQL数据类型:

数值类型、字符类型、时间日期类型

数值类型常用举例

字符类型常见举例

时间日期类型

案例

MySQL筛选条件

In null例子

select * from student where id is null;

错误例子: select * from student where is = null;(这样是显示不了的,空值以这种方式查询是查不出来的)

in not null例子

select * from student where id is not null;

与或非

and or not

and 例子:select * from student where id =5 andage = 9;

or  例子:select * from student where id =5 orage = 9;

排序(order by)

正序举例:select * from student order by age;

倒序举例:select * from student order by age desc;

限制(limit)

举例:select * from student limit 0,5;

(MySQL是 左闭右闭)

去重(distinct)

举例:select distinct * from student;

模糊查询

select * from 表名 where 表头字段 like ‘第一个字加%’;(查询任意多个字符 :%)

select * from 表名 where 表头字段 like ‘第一个字加__’(查询任意单个字符:__ ,后面几个字就带几个下划线_ )

注:也可查询任何位置关键字,如 名字中间字 或最后一个字。

范围查询

连续范围

select * from 表名 where 表头字段 >=范围1 and 范围2;

举例:

或、

select * from 表名 where 表头字段 between 范围1 and 范围2;

举例:

2.间隔范围

select * from 表名 where 表头字段 in (范围1, 范围2);

举例:

聚合函数

­­­统计个数:count()

select count(表头字段) from 表名;

求和:sum()

select sum(表头字段) from 表名;

最大值:max()

select max(表头字段) from 表名;

平均值:avg()

select avg(表头字段) from 表名;

最小值:min()

select min(表头字段) from 表名;

列出全部字段:gpoup_concat()

select group_concat(表头字段) from 表名;

group_concat大部分都是跟分组进行功能的共同使用,单独使用会非常少,在分组的情况下,只能够出现分组字段和聚合字段,其他的字段没有意义,会报错!

select group_concat(表头字段1),group_concat(表头字段2) from 表名;

where条件查询举例

select name,avg(age) from student where id<=5 group by name;

意思是在student表里where条件查询id小于等于5的人名以及年龄平均值。

聚合函数分组:

select group_concat(表头字段)from student group by表头字段;

(这样可把重复的数据打印在同一行上)

可一次查询多个数值

聚合筛选having

select 表头字段1,聚合函数(表头字段2) from 表名 group by 表头字段1 having 表头字段1 比较运算符 表头字段1的数值;

举例: select id,avg(age) from student group by id having id <= 2;

意思是利用having聚合筛选出student表里id小于等于2的平均年龄值的临时表,

注意表达格式!注意id出现的位置!

一个查询语句中同时包含了别名(as),聚合函数,where,having那么他们的执行顺序是

先是执行: where然后执行:聚合函数和别名最后执行:having

别名(as): 如果后面再用到聚合函数avg了可以进行聚合函数的赋值 ‘as 赋值名’

select id,avg(age) as aa from student group by id having id <= 2;

子查询

子查询相当于嵌套  将一个查询的结果留下来用于下次查询(select中嵌套select)

举例

mysql

表结构修改

1.修改表名:alter table 表格文件名 rename to 新文件名;

­­­­

2  .修改字段名:alter table 表格文件名 change 字段名 新字段名;

­

3 . 修改字段类型:alter table 表格文件名 modify mid 新字段类型;

4 . 添加字段:alter table 表格文件名 add 新字段名 数据类型(20);

5 . 删除字段:alter table 表格文件名 drop 字段名;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值