美文网首页
查询数据3种方法,排序查询,分组查询,嵌套查询,多表查询,分

查询数据3种方法,排序查询,分组查询,嵌套查询,多表查询,分

作者: 孤岛渔夫 | 来源:发表于2017-01-22 21:15 被阅读0次
    -- 查询数据1    查询表中的所有数据
-- * 通配符  代表所有字段
-- select * from 表名
select * from student;

-- 查询数据2  带条件式的查询
-- select * from 表名 where 条件表达式
select * from student where sex=2;
select * from student where sex=2 and age = 17;

-- 查询数据3  指定字段查询 (推荐 ..  效率高, 目的明确)
-- select `字段名1`, `字段名2`, .... `字段名N`  from 表名 [ where 条件表达式 ]
select id,name, age, sex from student where id > 2;

-- 排序查询
-- select 字段名 ...  from 表名 [wherer   ...]   order by 字段名 asc(默认升序)/desc(降序)
select id,name,sex,age from student order by age;

-- 分组查询
-- select 字段名  from 表名  group by 字段名
select `class`, max(`score`) from student group by `class`;

-- 嵌套查询
-- 查询每个班里面成绩最高的人
-- select `class`, name, max(`score`) from student group by `class`;

-- 查询每个班的最大成绩
select max(score) from student group by class;
-- 查询in条件里每个人的班级,姓名,成绩
select `class`,`name`,`score` from student where score in (90,59);

-- 把两个查询条件嵌套使用
select `class`,`score`,`name` from student where score in ( select max(score) from student group by class );


-- 多表查询
select name, hobby from student , life  where sid = studentId
 
-- 给不同的表 取别名(小名)      主要为了 区分 不同的表里有相同的字段名
select  s.id, name, sex, hobby
from student s, life l
where s.sid = l.studentId and sex=2


-- 分页查询
-- limit rows
-- limit offset,rows
--              offset 偏移量(从哪开始)
--              rows   行数(显示多少条数据)
select id,name,sex,regtime 
from student
limit 3;


select id,name,sex,regtime 
from student
limit 2,3;

select id,name,sex,regtime 
from student
limit 0,3;



create table if not exists `life`(
    `id` int unsigned  auto_increment primary key,
    `studentId` int(10) unsigned comment '学号',
    `hobby` varchar(20) default null comment '爱好'
)engine=MyISAM default charset=utf8;



insert into life values
(null,1,'唱歌'),
(null,1,'嫖'),
(null,1,'赌'),
(null,1,'抽'),
(null,1,'吹箫'),
(null,3,'学霸'),
(null,3,'拍片'),
(null,3,'撸'),
(null,3,'美女'),
(null,6,'看片'),
(null,6,'打手枪'),
(null,6,'打飞机'),
(null,6,'学习PHP')



相关文章

  • 查询数据3种方法,排序查询,分组查询,嵌套查询,多表查询,分

  • SQL查询语句1

    复杂查询 = 简单查询 + 限定查询 + 查询排序 + 多表查询 + 分组统计查询 + 子查询。 面对所有的复杂查...

  • Mysql 1.数据库进阶

    1.DQL:查询语句排序查询聚合函数分组查询分页查询2.约束3.多表之间的联系4.范式5.数据库的备份与还原 排序...

  • MySQL03

    DQL查询语句 分页查询 分组函数 分组查询 子查询 多表连接查询 1、分页查询 当数据库中数据过多时,不能一次全...

  • MySQL约束课堂笔记

    今日内容 一、DQL:查询语句2、排序查询3、聚合函数4、分组查询5、分页查询二、约束三、多表之间的关系 DQL:...

  • MySQL-6:查询语句

    1、DQL:查询语句: 1.排序查询 2.聚合函数 3.分组查询 4.分页查询 2、约束3、多表之间的关系4、范式...

  • mysql---多表查询

    判断数据表中的分数是否及格: 多表联合查询---左连接 统计个数 嵌套查询: 多表查询:

  • Python MySQL数据库2:数据库查询

    总体内容 一、数据准备、基本的查询 二、条件查询 三、排序 四、聚合、分组 五、分页 六、连接查询(多表的时候有用...

  • mysql基础-DQL查询(三)

    DQL查询分为:1.排序查询。2.聚合查询。3分组查询。4.分页查询 where 和having 的区别: 1.w...

  • SQL之DQL

    DQL用于从数据库查询数据,并不会修改数据 基本查询 条件控制 查询排序 分组查询 limit限制 一.基本查询 ...

网友评论

      本文标题: 查询数据3种方法,排序查询,分组查询,嵌套查询,多表查询,分

      本文链接:https://www.haomeiwen.com/subject/qavobttx.html