目录
(3)列子查询
(6)总结
一、构建数据库
1、概念
重点:DQL是我们每天都要接触编写最多也是最难的SQL,该语言用来查询记录,它不会修改数据库和表结构。
2、新建四张表
(1)student表
DROP TABLE IF EXISTS student;/*如果存在student表就删除*/ CREATE TABLE `student` ( `id` INT ( 10 ) PRIMARY KEY,/*主键*/ `name` VARCHAR ( 10 ), `age` INT ( 10 ) NOT NULL, `gender`VARCHAR(2) );
(2)course表
DROP TABLE IF EXISTS course; CREATE TABLE `course` ( `id` INT ( 10 ) PRIMARY KEY,/*主键*/ `name` VARCHAR ( 10 ), `t_id` INT(10) );
(3)teacher表
DROP TABLE IF EXISTS teacher; CREATE TABLE `teacher` ( `id` INT ( 10 ) PRIMARY KEY,/*主键*/ `name` VARCHAR ( 10 ) );
(4)scores表
DROP TABLE IF EXISTS scores; CREATE TABLE `scores` ( `s_id` INT ( 10 ), `score` INT ( 10 ), `c_id`int(10), PRIMARY KEY(s_id,c_id) );
(5)给四张表插入数据
insert into student (id,name,age,gender)VALUES(1,'张小明',19,'男'),(2,'李小红',19,'男'),(3,'小刚',24,'男'),(4,'小龙',11,'男'),(5,'小丽',18,'男'),(6,'张小军',18,'女'),(7,'小航',16,'男'),(8,'小亮',23,'男'),(9,'小杰',22,'女'),(10,'李小虎',21,'男'); insert into course (id,name,t_id)VALUES(1,'数学',1),(2,'语文',2),(3,'c++',3),(4,'java',4),(5,'php',null); insert into teacher (id,name)VALUES(1,'Tom'),(2,'Jerry'),(3,'Tony'),(4,'Jack'),(5,'Rose'); insert into scores (s_id,score,c_id)VALUES(1,80,1); insert into scores (s_id,score,c_id)VALUES(1,56,2); insert into scores (s_id,score,c_id)VALUES(1,95,3); insert into scores (s_id,score,c_id)VALUES(1,30,4); insert into scores (s_id,score,c_id)VALUES(1,76,5); insert into scores (s_id,score,c_id)VALUES(2,35,1); insert into scores (s_id,score,c_id)VALUES(2,86,2); insert into scores (s_id,score,c_id)VALUES(2,45,3); insert into scores (s_id,score,c_id)VALUES(2,94,4); insert into scores (s_id,score,c_id)VALUES(2,79,5); insert into scores (s_id,score,c_id)VALUES(3,65,2); insert into scores (s_id,score,c_id)VALUES(3,85,3); insert into scores (s_id,score,c_id)VALUES(3,37,4); insert into scores (s_id,score,c_id)VALUES(3,79,5); insert into scores (s_id,score,c_id)VALUES(4,66,1); insert into scores (s_id,score,c_id)VALUES(4,39,2); insert into scores (s_id,score,c_id)VALUES(4,85,3); insert into scores (s_id,score,c_id)VALUES(5,66,2); insert into scores (s_id,score,c_id)VALUES(5,89,3); insert into scores (s_id,score,c_id)VALUES(5,74,4); insert into scores (s_id,score,c_id)VALUES(6,80,1); insert into scores (s_id,score,c_id)VALUES(6,56,2); insert into scores (s_id,score,c_id)VALUES(6,95,3); insert into scores (s_id,score,c_id)VALUES(6,30,4); insert into scores (s_id,score,c_id)VALUES(6,76,5); insert into scores (s_id,score,c_id)VALUES(7,35,1); insert into scores (s_id,score,c_id)VALUES(7,86,2); insert into scores (s_id,score,c_id)VALUES(7,45,3); insert into scores (s_id,score,c_id)VALUES(7,94,4); insert into scores (s_id,score,c_id)VALUES(7,79,5); insert into scores (s_id,score,c_id)VALUES(8,65,2); insert into scores (s_id,score,c_id)VALUES(8,85,3); insert into scores (s_id,score,c_id)VALUES(8,37,4); insert into scores (s_id,score,c_id)VALUES(8,79,5); insert into scores (s_id,score,c_id)VALUES(9,66,1); insert into scores (s_id,score,c_id)VALUES(9,39,2); insert into scores (s_id,score,c_id)VALUES(9,85,3); insert into scores (s_id,score,c_id)VALUES(9,79,5); insert into scores (s_id,score,c_id)VALUES(10,66,2); insert into scores (s_id,score,c_id)VALUES(10,89,3); insert into scores (s_id,score,c_id)VALUES(10,74,4); insert into scores (s_id,score,c_id)VALUES(10,79,5);
二、单表查询
1、查询所有记录
基本语法(注意:在开发中,严禁使用select * from)
select * from 表名;
select * from student;
2、查询指定的列
select `id`, `name`,`age`, `gender` from student; select `id`, `name`, `age` from student;
3、去重
如果表中有完全重复的记录只显示一次,在查询的列之前加上distinct。
select DISTINCT `字段名` from `表名`;
4、列运算
select `id`,`name`,age/10 from student;
注意:
①我们写的所有的查询语句,最终执行的结果,都是生成一张虚拟表。
②null值和任何值做计算都为null ,null不意味着0。比如在员工表中,给工资都加1000,有工资的加减,有的员工工资初始值,设置了空值。
select id, `name`,sa1+1000 from emp emloyee;
null值和任何值做计算都为null ,需要用到函数ifnull()函数。如果薪资是空,则为0。将字符串做加减乘除运算,会把字符串当0处理。
select IFNULL(sal,0) + 1000 from employee;
5、别名
我们可以给列起别名, 因为我们在查询过程中,列名很可能重复,可能名字不够简洁,或者列的名字不能满足我们的要求。
select `id`as `编号`, `name` as`姓名^ ,`age`as `年龄` ,`gender`as `性别` from student ;
除了使用关键字 as 来给表或是 列起别名外,还可以直接使用空格字符达到同样的效果
<