前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >select基础查询

select基础查询

作者头像
秋名山码神
发布2022-12-13 15:21:49
2840
发布2022-12-13 15:21:49
举报
文章被收录于专栏:码神随笔码神随笔

select查询

请添加图片描述
请添加图片描述

distinct取消重复

代码语言:javascript
复制
create table student(
	id int not null default 1,
	name varchar(20) not null default '',
	chinese float not null default 0.0,
	english float not null default 0.0,
	math float not null default 0.0
)

insert into student(id,name,chinese,english,math) values (1,'ymm',89,78,99);
insert into student(id,name,chinese,english,math) values(2,'hh',99,89,88);
insert into student(id,name,chinese,english,math) values(3,'yxc',90,99,90);
  1. 查询表中所有学生的信息
  2. 查询表中所有学生的姓名和对应的英语成绩
  3. 过滤表中重复的数据 distinct
  4. 要查询的记录,每个字段都相同,才会去重
代码语言:javascript
复制
-- select 查询
select * from student;
select name,english from student;
select distinct * from student;
select distinct chinese from student;
select distinct name,chinese from student;
-- 要查询的记录,每个字段都相同,才会去重

使用表达式进行运算,使用as语句

请添加图片描述
请添加图片描述
  1. 统计每个学生的总分
  2. 在所有学生总分加10分的情况
  3. 使用别名表示学生的数学分数
代码语言:javascript
复制
-- 总分
select `name`,(chinese+english+math) from student;
select `name`,(chinese+english+math+10) from student;

select `name`,(chinese+english+math) as total from student;
select `name` as '名字',(chinese+english+math) from student;

在where子句中使用运算符

请添加图片描述
请添加图片描述
代码语言:javascript
复制
-- where
select * from student where `name` = 'ymm';
select * from student where `english` >90;
select * from student where (chinese+english+math) > 200;
代码语言:javascript
复制
-- 查询math>60 并且 english > 90
select * from student where `math`>60 and `english`>90;

-- 查询总分大于200并且math大于chinese的首字母为y的学生
select * from student where (chinese+math+english) > 200 and math>chinese and `name` like 'y%';

-- 查询English在80到90分之间的
select * from student where english between 80 and 90; -- 闭区间
select * from student where english >=80 and english <= 90;

使用order by子句排序查询

请添加图片描述
请添加图片描述

升序:Ascending order

降序:Descending order

代码语言:javascript
复制
-- 排序
select * from student order by math;

-- 总分降序
select * from student order by (chinese + math + english) desc;
-- 首字母为y升序
select * from student where `name` like 'y%' order by (chinese+math+english);

select `name`,(chinese+english+math) as total from student where `name` like 'y%' order by total;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-11-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • select查询
    • 使用表达式进行运算,使用as语句
      • 在where子句中使用运算符
        • 使用order by子句排序查询
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档