首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >mysql常用查询语句

mysql常用查询语句

作者头像
张哥编程
发布2024-12-13 16:48:35
发布2024-12-13 16:48:35
1.8K0
举报
文章被收录于专栏:云计算linux云计算linux

-- 创建表 create table stu( id int, name varchar(20), chinese double, english double, math double ); -- 插入记录 insert into stu(id,name,chinese,english,math) values(1,'tom',89,78,90); insert into stu(id,name,chinese,english,math) values(2,'jack',67,98,56); insert into stu(id,name,chinese,english,math) values(3,'jerry',87,78,77); insert into stu(id,name,chinese,english,math) values(4,'lucy',88,NULL,90); insert into stu(id,name,chinese,english,math) values(5,'james',82,84,77); insert into stu(id,name,chinese,english,math) values(6,'jack',55,85,45); insert into stu(id,name,chinese,english,math) values(7,'tom',89,65,30); # 1.最简单的查询,查询所有; *代表所有的列; select * from stu; # 2.查具体的列名,则select后面直接跟列名; select id,name,english from stu; # 3.去除重复数据; 针对后面多个列,完全相同的时候,去除重复数据; -- select DISTINCT 列名 from 表名; select DISTINCT name from stu; # 4.ifnull,原因在于英语为null,在mysql里面null+其他数据=null # ifnull(参数1,参数2) 如果参数1有值,则有参数1;如果参数1位null,则走参数2; # 查询id,name,总成绩 select id,name,(chinese+ifnull(english,0)+math) from stu; -- mysql会将 字符串'123'转成数字123,再和100加,加完之后是223 SELECT '123' + 100; -- 'abc'不能转为数字,只输出100; SELECT 'abc' + 100; -- null+数据=null select null+100; # 5.别名机制; # 5.1 列的别名;列名 as '别名' select id as '编号',name as '姓名', chinese as '语文成绩' from stu; # 5.2 可以省略as select id '编号',name '姓名', chinese '语文成绩' from stu; # 5.3 单引号省略 select id 编号,name 姓名, chinese 语文成绩 from stu; # 5.4 给表加别名; 一般用在多个表查询的时候,或表名 比较长的情况;mydbstudentscore select s.id,s.`name`,s.chinese from stu s; # 5.5 函数可以加别名; select CURRENT_DATE() 当前日期;

where条件查询语句

代码语言:javascript
复制
CREATE TABLE stu1 (
  id int,
  name varchar(20),
  age int,
  sex varchar(5),
  address varchar(100),
  math int,
  english int
);
-- 插入记录
INSERT INTO stu1(id,NAME,age,sex,address,math,english) VALUES 
(1,'马云',55,'男','杭州',66,78),
(2,'马化腾',45,'女','深圳',98,87),
(3,'马景涛',55,'男','香港',56,77),
(4,'柳岩',20,'女','湖南',76,65),
(5,'柳青',20,'男','湖南',86,NULL),
(6,'刘德华',57,'男','香港',99,99),
(7,'马德',22,'女','香港',99,99),
(8,'德玛西亚',18,'男','南京',56,65);

# 1.查询数学成绩>60分的学生信息;
select * from stu1 where math>60;
# 2.查询年龄不等于20的学生信息;
select * from stu1 where age<>20;
select * from stu1 where age!=20;
# 3.查询年龄在20到45之间的人员信息;
select * from stu1 where age>=20 && age<=45;
select * from stu1 where age>=20 and age<=45;
# 3.2 BETWEEN and方式; BETWEEN 小值 and 大值; 
select * from stu1 where age BETWEEN 20 and 45;
select * from stu1 where age BETWEEN 45 and 20;
# 4.地址是杭州或香港 或南京人员信息
# or ;not in/ in(集合)
select * from stu1 WHERE address='杭州' or address='香港' or address='南京';
select * from stu1 WHERE address='杭州' || address='香港' || address='南京';
select * from stu1 where address in('杭州','香港','南京');
select * from stu1 where address not in('杭州','香港','南京');
# 5.查询英语没考试的人员信息;english null,在mysql里面null不是0;
-- 对于null的数据,不能用=,也不是0
select * from stu1 where english is null;
# 6.模糊查询,like,比较常用;%:表示多个字符;
-- 6.1 查询所有姓马的人员信息;
select * from stu1 where name like '马%';
-- 6.2 查询所有以‘华’结尾的人员信息;
select * from stu1 where name like '%华';
-- 6.3 查询所有名字包含‘德’字的人员信息;
select * from stu1 where name like '%德%';
-- 6.4 查询所有以‘德’开头的人员信息,且名字长度为4的人员;
select * from stu1 where name like '德___';

排序查询

代码语言:javascript
复制
CREATE TABLE stu2 (
  id int,
  name varchar(20),
  age int,
  sex varchar(5),
  address varchar(100),
  math int,
  english int
);
-- 插入记录
INSERT INTO stu2(id,NAME,age,sex,address,math,english) VALUES 
(1,'马云',55,'男','杭州',66,78),
(2,'马化腾',45,'女','深圳',98,87),
(3,'马景涛',55,'男','香港',56,77),
(4,'柳岩',20,'女','湖南',76,65),
(5,'柳青',20,'男','湖南',86,NULL),
(6,'刘德华',57,'男','香港',99,99),
(7,'马德',22,'女','香港',99,99),
(8,'德玛西亚',18,'男','南京',56,65),
(9,'唐僧',25,'男','长安',87,78),
(10,'孙悟空',18,'男','花果山',100,66),
(11,'猪八戒',22,'男','高老庄',58,78),
(12,'沙僧',50,'男','流沙河',77,88),
(13,'白骨精',22,'女','白虎岭',66,66),
(14,'蜘蛛精',23,'女','盘丝洞',88,88);
# 1.按照年龄进行排序; 默认升序
select * from stu2 ORDER BY age; 
select * from stu2 ORDER BY age asc; 
select * from stu2 ORDER BY age desc; 
# 2.按照年龄先降序排序,数学成绩升序排列 ;
SELECT * from stu2 order by age desc,math asc;
# 3.针对所有的成绩做降序排列;
select name,(math+ifnull(english,0)) from stu2 
order by (math+english) desc;
-- 也可以使用别名进行排序;
select name,(math+ifnull(english,0)) 总成绩 from stu2 
order by 总成绩 desc;
-- 只查性别为男的人员信息的总成绩排序
select name,(math+ifnull(english,0)) 总成绩 from stu2 
where sex='男'
order by 总成绩 desc;
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-08,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档