数据库查询

-- SQL查询语句
--查询语句的语法:select 字段名,.... from 表名 [group by 分组 where 限定行 order by 排序] .....

--查询获得的数据是虚数据,不会改变数据库原有的数据,查询到的数据来源于原始表,通过一系列的查询语句操作得到结果,因此原始表不会改变。

创建如下表:
学生表student(学号,姓名,年龄,性别,班级,所在系,地址,出生日期) 
选课表xuanxiu(学号,课程号,成绩) 
课程表course(课程号,课程名称,教师姓名)     
create table student(
            student_id int PRIMARY KEY not null,
            student_name varchar(20) not null,
            student_age int not null,
            student_sex varchar(4) not null,
            student_classroom varchar(20) not null,
            student_dept varchar(30) not null,
            student_address varchar(30) not null,
            student_date date not null
            )
            create table xuanxiu(
            student_id int  not null,
            course_id int not null,
            student_score int not null,
            PRIMARY KEY(student_id,course_id)
            )
                create table course(
                course_id int primary key not null,
            course_name varchar(30) not null,
            course_teacher varchar(5) not null
            )

-- 查询所有学生的所有字段信息(获取表中所有行、所有列数据)
    select -- 查询关键字
        *  -- 要查询的列,"*"表示所有列(也可以查询一部分数据,但需要把查询内容列举出来,不能用*)
    from   -- 要查询的来自哪    
        student -- 要查询的表名

-- 查询学生"姓名"、"性别"、"地址"(查询表中的所有行、部分列数据)
    select
        student_name,
        student_sex,
        student_address
    from
        student

-- 查询学生"姓名"、"性别"、"地址"(别名显示用关键字as)
    select
        student_name as '姓名',
        student_sex as '性别',
        student_address as '地址'
    from
        student

-- 查询表中的学生的年龄(以别名stu_age显示)
    -- now():获得当前的时间
    -- year(date):获得date日期中的年

    select
        YEAR(NOW())-YEAR(student_date) as stu_age,
    from 
        student

-- 查询年龄在18到28岁之间的所有学生信息

select
       student. *
    from 
        student

where

YEAR(NOW())-YEAR(student_date) between 18 and 28

--sql中进行模糊查询的有_,%两个(其中_代表一个字符,%代表查询的是任意多字符可以是一个,也可以是许多个,使用like关键字而非=)

-- 查询姓名中带有"王"的学生(模糊查询)
    select
        *
    from
        student
    where
        student_name like '%王%'

-- 聚合查询,通过5个聚合函数进行查询(count,max,min,sum,avg)
-- 统计查询count()函数
-- 统计所有学生的数量
    select
        count(student_id) as '人数'     
    from                  
        student

--"*"表示任意列只要存在数据就参与统计,使用具体列名时,如  果某行数据中该列的值为null则不参与统计

 -- 如果要统计任意列数据,则尽量使用"主键列"进行统计(不使用*)以提高查询效率

-- 求最大值(max)和最小值(min)函数

-- 查询年龄的最大值
    select 
        max(student_age) as '最大年龄'
    from
        student
-- 查询年龄的最小值
    select 
        min(student_age) as '最小年龄'
    from
        student

-- 求和函数(sum)
-- 求学号总和
    select
        sum(student_id) as '学号总和'
    from
        student
-- 求平均数函数(avg)
-- 求班级中平均年龄
    select
        avg(YEAR(NOW())-YEAR(student_date)) as '平均年龄'
    from
        student

-- 查询所有学生信息,按学号倒叙排序(order by 排序关键字 排序规则)
-- 排序语句中默认的排序规则为升序(asc),降序(desc)
    select * from student order by student_id desc

-- 分组查询(group by 分组关键字)
-- 分组统计不来不同城市的学生数量
    select
        student_address as '城市',
        count(student_id) as '人数'
    from
        student
    group by student_address

下面是一些简单的练习题,仅供参考:

1.    查询年龄大于22岁的学生的学号和姓名。
select student_id,student_name from student where student_age>=22 

2.    查询“小王”所选修的全部课程代号。 
select course_id from course where course_id in (
select course_id from xuanxiu where student_id in(
select student_id from student where student_name='小王'
)
)
select s.student_name,x.course_id from student s INNER JOIN xuanxiu x on x.student_id=s.student_id where s.student_name='小王'
3.    查询各科成绩都在80分以上的学生姓名及所在系。
select s.student_name,s.student_dept from student s left join xuanxiu x on x.student_id=s.student_id WHERE x.student_score>80

4.    查询生日为空的所有学生。
select * from student where student_date is null 
5.    查询08E03班中的所有甘肃学生。
select * from student where student_address='甘肃' and student_classroom='08E03' 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值