栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

数据库的基本查询语句

数据库的基本查询语句

#创建数据库

create database stu2;

#创建utf8字符集的数据库

create database stu3 character set utf8;

#查看数据库列表

show databases;

#删除数据库

drop database stu3;

#使用数据库

use stu;

#创建老师表 #primary key:主键 auto_increment:自增长

create table teacher

(

id int(4) primary key auto_increment,

name varchar(20) not null,

sex char(1) not null,

age int(3)

);

#查看表

show tables;

#查看表定义

desc teacher;

#删除表

drop table teacher;

#插入一条数据

insert into info(name,sex,age)values('刘文龙','男',48);

#更新数据

update info set age=60 where name='刘文龙';

#删除数据

delete from info where id=2;

#查询所有信息

select*from info;

#查询部分字段

select id,name from info;

#查询编号为3的人编号,姓名,年龄

select id,name,age from info where id=3;

#查询编号为5的人编号,姓名,年龄(用中文显示)

select id as 编号,name 姓名 from info where id=3;

#查询“姓张”的人的姓名

select name from info where name like '刘%';

#查询“姓刘”和“姓桑”的人的姓名

select name from info where name like '刘%' or name like '桑%';

#查询年龄为空的人的姓名和年龄

select name,age from info where age is null;

#查询年龄不为空的人的姓名和年龄

select name,age from info where age is not null;

#查询年龄在20--80之间的人的姓名和年龄

select name,age from info where age between 20 and 80;

select name,age from info where age>=20 and age<=80;

select name,age from info where age>=20 && age<=80;

#查询所有年龄段的人的姓名和年龄

select name,age from info where age>=20 or age<=80;

#查询班级中名字是“杨瑞”的一个

select distinct name from info where name='杨瑞';

#查询姓名和年龄按年龄升序排列

select name,age from info order by age;

select name,age from info order by age asc;

#查询姓名和年龄按年龄降序排列

select name,age from info order by age desc;

#查询编号,姓名和年龄按年龄升序排列,如果年龄相同,再年龄相同的人的id按降序排列

select id,name,age from info order by age asc,id desc;

#查询前三条学生编号,姓名

select id,name from info limit 0,3;

select id,name from info limit 3;

#查询4,5,6条学生编号,姓名

select id,name from info limit 3,3;0

#查询学生姓名和成绩(内连接:跟查询位置无关,只要有成绩的人都能查出来)

select i.name,s.grade from info i inner join score s on s.sid=i.id;

select i.name,s.grade from score s inner join info i on s.sid=i.id;

#查询学生姓名和成绩(左外连接:跟位置有关,能查出来没有成绩的人)

select i.name,s.grade from info i left join score s on s.sid=i.id;

select i.name,s.grade from score s left join info i on s.sid=i.id;

#查询学生姓名和成绩(右外连接:跟位置有关,能查出来没有成绩的人)

select i.name,s.grade from info i right join score s on s.sid=i.id;

select i.name,s.grade from score s right join info i on s.sid=i.id;

#查询学生姓名,课程和成绩

select i.name,c.name,s.grade from info i

inner join score s on i.id=s.sid

inner join course c on c.id=s.cid;

#查询学生姓名,课程和成绩(笛卡尔积)

select i.name,c.name,s.grade from info i,score s,course c where i.id=s.sid and c.id=s.cid;

#求平均年龄

select avg(age) from info;

#求总年龄

select sum(age) from info;

#求总人数

select count(*) from info;

select count(id) from info;

select count(1) from info;

#求最大和最小年龄

select max(age),min(age) from info;

#查询学生所有课程的平均分

select cid,avg(grade) from score group by cid

#查询学生语文的平均分

select cid,avg(grade) from score where cid=1 group by cid

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/326804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号