基本使用 limit 数字
limit 后面跟上 数字,表示对返回的结果进行限制显示的个数。
比如 limit 1 表示只显示一条结果。
- 按照age 进行倒序,只显示第一条结果;
limit 的使用方式select * from studentsorder by age desclimit 1;

 
查询所有男同学,按照age 排序, 显示前5条数据。
select * from students where sex='男' order by age limit 5;查询出最高的分数是多少?
select score from students order by score desc limit 1;查询出最低的分数是多少?
select score from students order by score asc limit 1;limit m,n
在做查询结果的时候 limit 后面可以跟两个数字。

查询 第4-8条数据
select * from students 
limit 3,5;
查询第2-5 条数据
select * from students limit 1,4;查询第3条 (不是前3条)
select * from students limit 2,1;查询第2条 (不是前2条)
select * from students limit 1,1;查询第5-10条数据
select * from students limit 4,6;
经典问题
- 查找年龄最大的同学(有可能两个同学的年龄一样大。如果是这样的话,两个同学都要被查找到)
 
在解决这类问题的时候,首先学会分析问题:
- 因为可能有多人age 同样年纪的人比较多。
 如果我知道 最大的年龄是多少。
select age from students order by age desc limit 1;
根据年龄可以找到符合条件的所有人。
select * from students where age = 90;
解决这个问题 可以使用两条语句找到。但是最好一条解决。
两条sql 可以这样合并;
90 是从上面一句获取到的。
可以将 90 替换为上面的sql 语句。
select * from students
where age = 
    (SELECT age from students
            ORDER BY age desc
            LIMIT 1
        );

执行的时候 先执行 () 中的语句。
- 查找最小的年龄的人有哪些。
 
分析问题:
- 找到最小的年龄。
b. 根据年龄,找出符合年龄的所有人。select age from students order by age asc limit 1;select * from students where age = (select age from students order by age asc limit 1); 
- 找出成绩最高的所有学员。
 
分析问题:
先找到最高的成绩;
select score from students order by score desc limit 1;根据成绩找到对应的人员。
select * from students where score = (select score from students order by score desc limit 1);
- 查找数学成绩最高的学员。
 
分析问题:
找到最高的数学分数。
select score from students where course="数学" order by score desc limit 1;根据分数找人。
select * from students where course="数学" and score = (select score from students where course="数学" order by score desc limit 1);
思维导图
作业
根据students表进行如下操作
- 查询姓名为三个字的同学 并按照age 排序;
 - 查询所有姓王 有数学成绩的同学信息;
 - 以语文成绩从大到小进行排序;
 - 按照age 降序,score 升序进行排序;
 - 男生同学的数学成绩,按照score 降序,age升序排序;
 - 查询男生的前5条
 - 查找男生中 第2-5 条数据
 - 查找男生中成绩最高分是多少 (分数)
 - 查找女生中成绩最低分是多少 (分数)
 - 分数最高的同学 (所有符合条件的同学信息)
 - 分数最低的同学 (所有符合条件的同学信息)
 - 语文成绩最高的同学
 - 数学成绩最低的男同学
 - 分数第二高的同学
 数学分数第二高的同学
查询姓名为三个字的同学 并按照age 排序;
select * from students where username like "___" order by age- 查询所有姓王 有数学成绩的同学信息;
SELECT * FROM students WHERE username like '王%' ANd course='数学'; - 以语文成绩从大到小进行排序;
select * from students where course="语文" order by score desc - 按照age 降序,score 升序进行排序;
SELECT * FROM students ORDER BY age DESC, score asc; - 男生同学的数学成绩,按照score 降序,age升序排序;
select * from students where sex="男" and course="数学" order by score desc, age asc - 查询男生的前5条
SELECT * FROM students WHERE sex='男' LIMIT 5; - 查找男生中 第2-5 条数据
select * from students where sex="男" limit 1,4 - 查找男生中成绩最高分是多少 (分数)
SELECT score FROM students WHERE sex='男' ORDER BY score desc LIMIT 1; - 查找女生中成绩最低分是多少 (分数)
select score from students where sex="女" order by score asc limit 1; 分数最高的同学 (所有符合条件的同学信息)
SELECT * FROM students WHERE score =(SELECT score FROM students ORDER BY score desc LIMIT 1);分数最低的同学 (所有符合条件的同学信息)
select * from students where score=(select score from students order by score asc limit 1);- 语文成绩最高的同学
SELECT * FROM students WHERE course='语文' and score=(SELECT score FROM students WHERE course='语文' ORDER BY score desc LIMIT 1); - 数学成绩最低的男同学
select * from students where sex="男" and course= "数学" and score= -- 最低分 (select score from students where course="数学" and sex= "男" order by score asc limit 1) - 分数第二高的同学
SELECT * FROM students WHERE score=(SELECT DISTINCT (score) from students ORDER BY score desc LIMIT 1,1); - 数学分数第二高的同学
select * from students where course="数学" and score=(select distinct (score) from students where course="数学" order by score desc limit 1,1) 
下面根据 jobs 表进行操作
- 按照工作年限(workyear)进行降序排序;
select * from jobs order by workyear desc; - 查询符合如下条件之一的同学
a. 本科学历 3年及以上工作经验
b. 专科学历 4年及以上工作经验select * from jobs where (degree = '本科' and workyear >=3) or (degree = '专科' and workyear >=4); - 按学历统计人数。
select degree ,count(*) from jobs group by degree; - 工作年限最高的学历为专科的人员。
select * from jobs where degree = '专科' and workyear = (select workyear from jobs where degree ='专科' order by workyear desc limit 1); - 工作年限最低的人员信息;
select * from jobs where workyear =(select workyear from jobs order by workyear limit 1); - 工作年限去重。
select distinct (workyear) from jobs; 
Linux作业
- 统计 /etc/man_db.conf 文件中有多少个 
man字符? 
使用grep 命令
https://www.runoob.com/linux/linux-comm-grep.html
grep -o "man" /etc/man_db.conf | wc -l
使用vi命令
:%s/man//gn


查看服务器的端口号使用什么命令?
netstat -anpt将 /var/log/messages 文件中的第10-90 行内容 保存到 /tmp/log.txt
cat -n /var/log/messages | head -90 | tail -81 > /tmp/log.txt
git作业
将上面的作业整理成文档 提交到自己的 仓库里面。

