MySQL学习
sql语句分类:
DQL(数据查询语言):查询语句,凡是select语句都是DQL
DML(数据操作语言):insert delete update ,对表当中的数据进行增删改
DDL(数据定义语言):create drop alter,对表结构进行增删改。
TCL(事务控制语言):commit提交事务,rollback回滚事务 T是Transaction 事务
DCL(数据控制语言):grant授权、revoke撤销权限等。
DOS命令:
1、登录MySQL : mysql -uroot -proot
2、展示数据库:show databaases;
3、创建数据库:create database demo01;
4、使用数据库:use demo01;
5、删除数据库:drop database demo01;
6、初始化数据:source sql脚本路径;
7、展示数据库中的表:show tables;
8、查看表结构:desc tb_books;
9、查看表数据:select * from tb_books; 实际开发中不建议使用*,效率较低
10、查询当前使用的数据库:select database();
11、查看当前使用数据库的版本:select version();
12、结束一条语句:\c
13、退出mysql:exit
14、查看其他数据库中的表:show tables from 数据库名;
15、查看创建表的语句:show create table 表名;
16、删除表:drop table 表名;
17、查看sql语句的执行计划/情况:explain sql语句;
SQL语句:
简单的查询语句(DQL):
select 字段名1,字段名2,,字段名3... from 表名;
提示:
1、任何一条sql语句以";"结尾
2、sql语句不区分大小写
3、字段可以参与数学运算 select id*2 from 表名;
4、给查询结果的字段名重命名 select id*2 as double from 表名; as可以省略
5、sql语句中字符串用单引号括起来 '姓名 ’ 虽然支持双引号或者不加引号 但是在oracle等其他数据库管理系统里面不通用里面
条件查询:
select 字段1,字段2... from 表名 where 条件; 执行顺序:先from,然后where,最后select
支持的运算符: 只要NULL参与的运算,结果一定是NULL(针对所有数据库)
查询名字中含有字母O的:
select name from 表名 where name like '%O%';
查询名字中第二个字母是A的:
select name from 表名 where name like '_A%';
查询名字中第三个字母是A的:
select id,,name from 表名 where name like ''%__A%'';
查询名字中有下划线的: _前面加转义字符\,把代表一个字符的下划线转义成普通的下划线
select id,name,phone from 表名 where name like '%\_%';
查询名字最后一个字母是T的: 第一个字母为T:‘T%’
select name from 表名 where name like '%T';
排序(升序、降序):指定语句:order by 默认是升序(asc),降序(desc)
按价格升序,查询姓名和价格:
select name,price from 表名 order by price;
指定升序:
select name,price from 表名 order by price asc;
指定降序:
select name,price from 表名 order by price desc;
价格升序,价格相同时,按名字降序排: 越靠前的字段越能起到主导作用,只有前面字段无法排序的时 候,才会启用后面字段
select name,price from 表名 order by price asc, name desc;
还可以按字段位置排:比如price在第三个位置,title在第一个位置 不建议使用,如果字段位置发生改变,那么sql语句也得改变,代码不健壮
select title,content,price from t_article order by 3 asc;
select select
* 3 * 5
from from
表名 1 表名 1
where where
条件 2 条件 2
order by group by
...; 4 order by 最后执行 ... 3
having
... 4
order by
... 6
名字里面有“java”,价格升序排列:
select title,content,price,type from t_article where content like '%java%' order by price;
类别是“前端”,价格降序排列:
select title,price,type from t_article where type = '前端' order by price desc;
类别是“前端”,并且概述含有”前端“,价格升序排列:
select title,price,type from t_article where type ='前端' and content like '%前端%' order by price asc;
分组函数/多行处理函数(输入多行,输出一行,自动忽略NULL):
规则:分组函数不可直接使用在where字句当中
count 计数/取得记录数 ; sum 求和; avg 平均值; max,min 最大、最小值;都是对某一组数据进行操作
组合起来用也是可以的:
select count(amount),sum(amount),max(amount),min(amount),avg(amount) from t_article;
价格总和:
select sum(price) from t_article;
查询id最大值的,id和名字:
select max(id),title from t_article;
查询价格平均值:
select avg(price) from t_article;
查询课程总数/记录总数:
select count(*) from t_article; //总记录条数
select count(title) from t_article; //title字段不为NULL的记录条数
查询count的数量9,忽略count为NULL,输出数量7:
select count(content) from t_article;
单行处理函数(输入一行,输出一行;处理完一行之后处理下一行):
查询价格*12的每一行数据:
select title,price*12 as 'double' from t_article;
查询(价格+数量)*12的每一行数据:
select title,(price+amount)*12 as 'double' from t_article;
ifnull()函数来改进:
select title,(ifnull(price,0) + amount)*12 as 'double' from t_article;
ifnull() 空处理函数: ifnull(可能为NULL的数据,被当做什么处理)
查询价格为空时