mysql基本查询知识

文章目录

mysql 的基本知识

登录数据库之前的反思:

登录数据库mysql
mysql -u root -p

查询数据库中有哪些子数据库
show databases;

查询某个数据库中有哪些表
use mydb3;
show tables;

删除指定数据库
drop database sys;
show databases;

删除当前数据库中的某张表(无emp3表了)
drop table emp3;

删除表的数据信息,保留表结构(有emp3表,数据列全为NULL)
delete from emp3;

查看某张表的详细信息结构
describe emp3;

查看某张表的数据信息
select * from user1;

创建表操作

创建一个数据库
create database test_1;

使用创建的数据库
use test_1;

创建一张基本结构表
 create table if not exists stu(
    -> sid int,
    -> sname varchar(20),
    -> gender varchar(5),
    -> age int,
    -> birth date,
    -> address varchar(20),
    -> score double
    -> );

给表指定主键以及其他说明字段(主键+自增长)--默认自增长1开始
 create table if not exists user1(
    -> name varchar(20),
    -> deptId int primary key auto_increment,
    -> salary double
    -> );
insert into user1 values('张三',NULL,156),('李四',NULL,6789),('李四',NULL,8798);

指定自增长开始的字段:
 create table if not exists user2(
    -> name varchar(20),
    -> deptId int primary key auto_increment,
    -> salary double)auto_increment = 100 ;

 insert into user2 values('张三',NULL,698),('李四',NULL,7809),('李四',NULL,817);

查询表操作

查询表的全部信息
select * from user2;

查询表的某列或者某几列信息
 select name , chinese from student;

对表进行设置别名进行查询数据

使用视图功能对表的数据进行数据展示

对表的数据进行分组,排序

对多张表进行联合查询数据

指定数据区间查询
select * from student where english >= 80 and english <=90; select * from student where english between 80 and 90;
不在区间内
 select * from student where not (english >= 80 and english <=90);
 select * from student where not (english  between 80 and 90);

模糊匹配字符进行查询(查询姓李的学生英语成绩)(匹配查询)
select name,english from student where name like '李%';

”或“条件的数据查询(或 查询)
select * from student where english=89 || (chinese+english+math)>200;

对姓李的学生的总成绩进行排序,降序输出(排序查询 desc asc)
 select * from student where name like '李%' order by (chinese+math+english) desc;
 
查询数据不是某些数的数据(成绩不为x,y)
 select * from student where not math in (89,90,91);
 
查询数据是某些数据的数据(成绩为x,y 的数)
select * from student where math in (89,90,91);

查询时候做分组统计,然后排序输出(男生,女生分别有多少人,根据两组人数进行降序,查询出人数大于4的性别的人数信息)
 select gender,count(*)  total_cnt from student group by gender having total_cnt > 4 order by total_cnt desc;

查询表时候对表进行统计计算 ,且对计算列设别名
select name,(chinese+english+math) total_score from student  where (chinese+english+math)>200;

对某些列设置别名进行查询的结果展示
 select name , chinese '语文成绩',english '英语成绩',math '数学成绩' from student;

不在10号部门工作的员工信息
 select * from emp where deptno != 10 order by empno asc;
 
第二个字母不是A,年薪降序,ifnull 如果comm是null,就取值为0
select  * from emp  where ename not like '_A%' and sal>1000 order by (sal*12 + ifnull(comm,0)) desc;
_匹配单个字符	
-- %用来匹配任意字符

每个部门,每个岗位的最高薪水
select deptno,job ,max(sal) from emp group by deptno,job order by deptno desc;

平均薪水大于1500的部门编号,按部门平均薪水降序
select deptno,avg(sal) avg_sal from emp group by deptno having avg_sal > 2000;

有奖金的员工的姓名,工资
 select ename ,sal from emp where comm is not null;
 
求薪资差距
 select max(sal)-min(sal) '薪资差距' from emp;

别名查询
1,表别名
select p.pid,p.pname from product as p;
2,列别名
select pname as '商品名', price as '商品价格'  from product;

去除重复查询
1,去除重复行
select distinct * from product;
2,去除重复列
 select distinct pname from product;

查询第二个字为‘寇’的所有商品
 select * from product where pname like '_蔻%';  --  _匹配单个字符
 
 查询含有‘鞋’字的所有商品
 select * from product where pname like '%鞋%';  -- %用来匹配任意字符
 
 查询category_id不为null的商品
 select * from product where category_id is not null;
 
使用least()求最小
select least(4,7,9,234) least_number;

使用greatest () 求最大值
select greatest(5,78,9,2134) big_number;

先价格排序,在根据分类属性排序
select * from product order by price desc,category_id desc;

查询价格大于200的商品的总条数
select count(pid) from product where price>200;

分组查询,统计各个分类商品的个数(分组之后select后边的字段只能写分组字段和聚合函数)
select category_id,count(pid ) from product group by category_id;

统计各个分类商品的个数,且只显示个数大于4的信息
select category_id,count(pid)  cnt from product group by category_id having cnt>4 order by cnt desc;

只查询前5条数据
 select * from product limit 5;

指定数据区间进行查询(从第4条开始,显示后面的5条数据--4~8  )
select * from product limit 3,5;

将一张表的数据导入到另外一张表
insert into product2 select pname,price from product;

以分组统计个数
 insert into product3 select  category_id,count(*) from product group by category_id;


PS:SQL执行顺序
from - group by - count  - select - having  - order by



增:

增加一个数据库
create database mydb11;

增加一张表(创建一张表)
create table if not exists product(
  pid int primary key auto_increment,
	pname varchar(20) not null,
	price double,
	category_id varchar(20)
);
  
给表增加某行数据
insert into product values(null,'啄木鸟衬衣', 300,'c002');

给表指定单元插入数据(指定列名)
 insert dept(deptno,name) values(78,"行政部");

删:

删除某个数据库
drop database test_1;

删除整张表,没有表结构
 drop table us;

删除某张表
delete from stu;   (表基本结构还在,只是删除了数据)
truncate stu;  (删除表,并创建了新表,结构与原来的表结构一样)
 
删除表的指定元素数据
delete from user1 where salary=2999;

PS:
-- delete 删除数据后,自增长还是在最后一值基础上加1
delete from t_user1;
insert into t_user1 values('张三',NULL,5000),('李四',NULL,5000),('李四',56,5900);

-- truncate删除之后,自增长从1开始
truncate t_user1;
insert into t_user1 values('张三',NULL,5000),('李四',NULL,5000),('李四',56,5900);


改:

更新表的某行

更新表的某列

更新表的某个元素值

过滤表中重复的元素
 select distinct * from student;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值