mysql 简单使用

查看命令

show databases; # 查看数据库
show tables;    # 查看表
show columns from table; # 查看列
desc table;     # 查看列
show status;    # 查看mysql状态信息
show create database/table; # 查看建库/表语句
show grants;    # 查看授权用户
show errors/warnings; # 查看error, warning信息

查数据

  1. 同时使用order by 与where子句时, order by子句应放于where子句后
  2. AND 与 OR 同时使用时, 先计算AND, 若想先计算OR 可以()
  3. group by 必须出现在where子句之后, 在order by子句之前
# 简单查询
select column1, column2 from table; # 查看多列数据
select * from table; # 查看所有列数据
select  distinct id from table; # 查询不同的ID
select * from table limit 5; # 查询前5条数据
select * from table limit 5, 10; # 查询从第5行开始的前10条数据

# 排序
select * from table order by column1; # 按column1升序排列
select * from table order by column1 ASC; # 按column1升序排列
select * from table order by column1 desc; # 按column1降序排列
select * from table order by column1, column2 desc; # 按column1升序排列后再按column2降序排列

# 数据过滤

# 操作符
= : 等于
<> : 不等于
!= : 不等于
< : 小于
<= : 小于等于
> : 大于
>= : 大于等于
between : 在两个数值之间

# 单条件
select * from table where id=1; # 查找表中, id=1的数据
select * from table where id between 10 and 20; 查找表中id为10-20之间的数据(包括10, 20)
select * from table where column1 is null; # 查找表中column1为Null的数据

# 使用操作符过滤
select * from table where id>10 and column1 is not null; # 查找表中id>10且column不为Null的数据
select * from table where id>10 or column1 is not null; # 查找表中id>10或column不为Null的数据
select * from table where id in (1,2,3); # 查找表中id为1、2、3的数据

3¤
% 匹配任意字符出现任意次数
_ 匹配单个任意的字符
select * from table where column1 like 'Dear%'; # 查找表中column1是以Dear开头的数据
select * from table where column1 like '%Dear%'; # 查找表中column1包含字符Dear的数据
select * from table where column1 like 'D_ar'; # 查找表中column1字段是4个字符的,其中以D开头, ar结束

# 使用正则表达式过滤
# 详情参考正则表达式
select * from table where id regexp '[0-9]'; # 查找表中id为0至9的数据

# 拼接字段
select (name, 'is in class ' ,class) from student_info; # 把查询出来的结果拼接起来形成新的字符串

# 使用别名
select (name, 'is in class ' ,class) as info from student_info;

# 字段计算
select  item, inventory, price, inventory*price as total from items_info;

# 文本处理函数
RTrim()  去掉右边的空格
LTrim()  去掉左边的空格
Trim()   去掉左右两边的空格
Upper()  将文本转为大写字符
Lower()  将文本转为小写字符
Left()   返回文本最左边的字符
Right()  返回文本最右边的字符
Length() 返回文本长度

# 日期处理函数
AddDate() 增加一个日期(天、周等)
AddTime() 增加一个时间(时、分等)
CurDate() 返回当前日期
CurTime() 返回当前时间
Date()    返回日期时间和日期部分
Datediff() 计算两个日期的差
Date_Format() 返回一个格式化的日期或时间串
DayOfWeek() 返回日期对应的是星期几
Time()    返回一个日期时间的时间部分
Year()    返回一个日期的年份部分
Month()   返回日期的月数部分
Day()     返回日期的天数部分
Hour()    返回日期的小时部分
Minute()  返回日期的分钟部分
Second()  返回日期的秒数部分
Now()     返回当前时间

# 数值处理函数
Abs()     返回一个数的绝对值
Sqrt()    返回一个数的平方根
Exp()     返回一个数的指数值
Mod()     返回除操作的余数
Pi()      返回圆周率
Rand()    返回一个随机数
Sin()     返回一个角度的正弦
Cos()     返回一个角度的余弦
Tan()     返回一个角度的正切

# 汇聚函数
AVG()     返回某列的平均值
MAX()     返回某列的最大值
MIN()     返回某列的最小值
SUM()     返回某列的值的总和
COUNT()   返回某列的行数

# 数据分组
select class, count(*) from student_info group by class # 查看每个班级有多少学生
select class, count(*) from student_info group by class having count(*) >40 # 查看班级人数大于40人的班级
# where 过滤行数据, having 过滤分组数据

posted on 2018-06-05 10:19  Dear、  阅读(123)  评论(0编辑  收藏  举报

导航