美文网首页
2019-08-21 单表查询

2019-08-21 单表查询

作者: Betterthanyougo | 来源:发表于2019-08-21 11:40 被阅读0次

--单表查询

select * from student;

select Sno,Sname from Student;

--算术表达式

--查全体学生的姓名及其出生年份。

select Sno,Sname,Sage,2019-Sage from Student;

select year(now())

select Sno,Sname,Sage,year(now())-Sage from Student;

--字符串常量

select Sname,'年龄:',Sage from student;

--函数:字符串函数,日期函数,数学函数

select Sname,'年龄:',Sage ,sdept,LOWER(sdept) from student;

--列别名

select Sname,'年龄:' as 年龄,Sage from student;

select Sname as 姓名,Sage 年龄 from student;

--年龄+10

select Sname as 姓名,Sage+10 年龄 from student;

-- 刘晨  +10,other 不变

select Sname as 姓名,Sage+10 年龄 from student where sname='刘晨'

union

select Sname as 姓名,Sage 年龄 from student where sname<>'刘晨'

-- 》=19 成年,《19 未成年 ???

SELECT * from sc

select Sno from sc;

select all Sno from sc;

-- 消除取值重复的行

select DISTINCT sno from sc;

--查询满足条件的行:where

select * from student  where sage =18

select * from student  where sage >=19

select * from student  where sage !=18

select * from student  where sage <>18

--确定范围  [19,20]

select * from student  where sage  BETWEEN 19 and 20

--确定范围  [18,20)

select * from student  where sage>=18 and sage<20

确定集合 谓词:IN <值表>, NOT IN <值表>

select * from student  where sdept in ('CS','IS')

select * from student  where sdept NOT in ('CS','IS')

字符匹配  谓词:[NOT] LIKE ‘<匹配串>’

匹配串为含通配符的字符串:% _

-- SDEPT 包含 S

SELECT * from student where sdept like '%S%'

SELECT * from student where sdept like 'S%'

SELECT * from student where sno like '%23'

SELECT * from student where sname like '张__'

SELECT * from student where sname like '张_'

涉及空值的查询

is null

查询没有先修课的课程

select * from course  where cpno is null

select * from course  where cpno is not null

多重条件查询

select * from student  where ssex='男' and sage=20 and sdept='cs'

select * from student  where ssex='男' and ssex='女'

select * from student  where ssex='男' or  sdept='ma'

select * from student  where ssex='男' or  sdept='ma' and sage between 18 and 19

21,25  23

21,23,25==>

21

23

25

select * from student  where ssex='男' or  (sdept='ma' and sage between 18 and 19)

select * from student  where (ssex='男' or  sdept='ma') and sage between 18 and 19

21,23,25==>

23

25

select * from student  where ssex='男'

ORDER BY子句

select * from student order by sage desc

select * from student order by sage

select * from student order by sage asc

select * from student order by sage ,sno desc  --年龄升序,学号降序

23,22,25,21

聚集函数

SELECT Count(*) from student

SELECT avg(Sage) from student

SELECT sum(Sage) from student

SELECT max(Sage) from student

SELECT min(Sage) from student

GROUP BY子句

select ssex  男女生,count(ssex)  人数 from student group by ssex

order by count(ssex) desc

select sage  年龄,count(sage)  人数 from student group by sage

order by 人数 desc

=======================================

男女生  人数

男           2

女           2

=======================================???

HAVING

--显示 各个年龄段 人数>=2 的统计结果

select sage  年龄,count(sage)  人数 from student group by sage

having count(sage)>=2

相关文章

  • 2019-08-21 单表查询

    --单表查询 select * from student; select Sno,Sname from Stude...

  • spring-data-jpa 复杂查询:使用

    单表查询 多表查询

  • 单表数据查询

    单表查询示例Student表: Student表 Course表 SC表 查询若干列 查询指定列 查询Studen...

  • 延迟加载

    意义 在进行数据查询时,为了提高数据库查询性能,尽量使用单表查询,因为单表查询比多表关联查询速度要快。 如果查询单...

  • SQL常用操作

    1、单表查询 SELECT 基本信息表.姓名,基本信息表.性别FROM 基本信息表 2、单表条件查询 SELECT...

  • SQL查询单表数据(一)

    本节讲述 基本的 select 查询单表数据语句 1 从单表中查询所有的行和列 查询表中所有的数据 在 SQL 中...

  • Python学习笔记十九(MySQL、SQL、查找、单表查询)

    查找 查找分为单表查询与多表查询 单表查询 查看现有数据表 查看所有数据 查看某些字段 比如我只关心title 字...

  • 查询SQL

    单表查询: 常规查询: SELECT 列名 From 表名 去重式查询: DISTINCT SELECT DIST...

  • mysql表格查询命令

    全表查询 语法: Select * from 表名称; 描述: 查询指定表中的所有数据 案例: 单条件查询 语法:...

  • Mysql索引优化

    1、单表索引优化 单表索引优化分析 创建表 建表 SQL 表中的测试数据 查询案例 查询category_id为1...

网友评论

      本文标题:2019-08-21 单表查询

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