1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > MySQL 09 DQL → select 初识查询数据和别名的使用

MySQL 09 DQL → select 初识查询数据和别名的使用

时间:2019-12-03 05:59:01

相关推荐

MySQL 09 DQL → select 初识查询数据和别名的使用

4.1 DQL → select 查询数据和别名的使用

DQL:Data Query LANGUAGE(数据查询语言)

所有的查询操作都用它 select简单的查询,无论多么复杂的查询它 都可以做到!数据库中最核心的语言,最重要的语句在实际开发中,查询数据的使用频率也确实是最高的。

先把下面的 school.sql 导入到 数据库软件里面生成一下!

create database if not exists `school`;-- 创建一个school数据库use `school`;-- 创建学生表drop table if exists `student`;create table `student`(`studentno` int(4) not null comment '学号',`loginpwd` varchar(20) default null,`studentname` varchar(20) default null comment '学生姓名',`sex` tinyint(1) default null comment '性别,0或1',`gradeid` int(11) default null comment '年级编号',`phone` varchar(50) not null comment '联系电话,允许为空',`address` varchar(255) not null comment '地址,允许为空',`borndate` datetime default null comment '出生时间',`email` varchar (50) not null comment '邮箱账号允许为空',`identitycard` varchar(18) default null comment '身份证号',primary key (`studentno`),unique key `identitycard`(`identitycard`),key `email` (`email`))engine=myisam default charset=utf8;-- 创建年级表drop table if exists `grade`;create table `grade`(`gradeid` int(11) not null auto_increment comment '年级编号',`gradename` varchar(50) not null comment '年级名称',primary key (`gradeid`)) engine=innodb auto_increment = 6 default charset = utf8;-- 创建科目表drop table if exists `subject`;create table `subject`(`subjectno`int(11) not null auto_increment comment '课程编号',`subjectname` varchar(50) default null comment '课程名称',`classhour` int(4) default null comment '学时',`gradeid` int(4) default null comment '年级编号',primary key (`subjectno`))engine = innodb auto_increment = 19 default charset = utf8;-- 创建成绩表drop table if exists `result`;create table `result`(`studentno` int(4) not null comment '学号',`subjectno` int(4) not null comment '课程编号',`examdate` datetime not null comment '考试日期',`studentresult` int (4) not null comment '考试成绩',key `subjectno` (`subjectno`))engine = innodb default charset = utf8;-- 插入学生数据 其余自行添加 这里只添加了2行insert into `student` (`studentno`,`loginpwd`,`studentname`,`sex`,`gradeid`,`phone`,`address`,`borndate`,`email`,`identitycard`)values(1000,'123456','张伟',0,2,'13800001234','北京朝阳','1980-1-1','text123@','123456198001011234'),(1001,'123456','赵强',1,3,'13800002222','广东深圳','1990-1-1','text111@','123456199001011233');-- 插入成绩数据 这里仅插入了一组,其余自行添加insert into `result`(`studentno`,`subjectno`,`examdate`,`studentresult`)values(1000,1,'-11-11 16:00:00',85),(1000,2,'-11-12 16:00:00',70),(1000,3,'-11-11 09:00:00',68),(1000,4,'-11-13 16:00:00',98),(1000,5,'-11-14 16:00:00',58);-- 插入年级数据insert into `grade` (`gradeid`,`gradename`) values(1,'大一'),(2,'大二'),(3,'大三'),(4,'大四'),(5,'预科班');-- 插入科目数据insert into `subject`(`subjectno`,`subjectname`,`classhour`,`gradeid`)values(1,'高等数学-1',110,1),(2,'高等数学-2',110,2),(3,'高等数学-3',100,3),(4,'高等数学-4',130,4),(5,'C语言-1',110,1),(6,'C语言-2',110,2),(7,'C语言-3',100,3),(8,'C语言-4',130,4),(9,'Java程序设计-1',110,1),(10,'Java程序设计-2',110,2),(11,'Java程序设计-3',100,3),(12,'Java程序设计-4',130,4),(13,'数据库结构-1',110,1),(14,'数据库结构-2',110,2),(15,'数据库结构-3',100,3),(16,'数据库结构-4',130,4),(17,'C#基础',130,1);

查询该表所有的数据

select * from `表名`

* 的意思是 我没有任何的限制条件,直接 把所有数据都查询出来即可。

查询该表里指定字段的数据

select `字段名1`,`字段名2` from `表名`

可以给字段取别名

select `字段名1`,`字段名2` AS `别名` from表名

有的时候,我们 所谓的字段名,并不是那么 见名知意。特别是我们 对于我们中国人来说,所以 我们 要 常起 别名。

可以给表取别名

select `字段名1`,`字段名2` AS `别名` from表名AS `别名`

函数 --> 字符串拼接(将搜索出来的数据与指定的字符串进行拼接显示

select concat(‘姓名:’,`字段名`) AS `新名字`

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。