为了账号安全,请及时绑定邮箱和手机立即绑定

<导图>Mysql常用查询语法

标签:
Java MySQL

普通查询

查看整个表

  • 格式:

    • select * from 表名;

  • 示例:

    • select * from students;

查询指定字段

  • 格式

    • select 字段名1,字段名2 from 表名;

  • 示例

    • select id, name from students;

给字段起别名

  • 格式

    • select 字段名1 as 新名字,字段名2 as 新名字 from 表名;

  • 示例

    • select name as 姓名, age as 年龄 from students;

消除重复行

  • 格式

    • select distinct 字段名 from 表名;

  • 示例

    • select distinct gender from students;

条件查询

比较运算

  • 格式

    • select 字段集合 from 表名 where 所需数据的范围;

  • 示例

    • select * from students where id > 5;

正则

  • 格式

    • select 字段集合 from 表名 where name rlike "正则表达式";

  • 示例

    • select * from students where name rlike "^张";

范围

  • 连续

    • select * from students where id between 3 and 6;

    • select 字段集合 from 表名 where 字段名 between 点值 and 点值;

    • 格式

    • 示例

  • 非连续

    • select * from students where id in (5, 8 , 9);

    • select 字段集合 from 表名 where 字段名 in 不连续"值";

    • 格式

    • 示例

数据排序

正序 asc

  • 格式

    • select 字段集合 from 表名 where 所需数据的范围 order by "作为排序标准"的字段名 asc;

  • 示例

    • select * from students where id >5 order by age asc;

反序 desc

  • 格式

    • select 字段集合 from 表名 where 所需数据的范围 order by "作为排序标准"的字段名 desc;

  • 示例

    • select * from students where id >5 order by age desc;

聚合函数

总数 count

  • 格式

    • select count(字段集合) from 表名;

  • 示例

    • select count(*) from students;

最大值 max

  • 格式

    • select max(字段名) from 表名;

  • 示例

    • select max(age) from students;

最小值 min

  • 格式

    • select min(字段名) from 表名;

  • 示例

    • select min(age) from students;

求和 sum

  • 格式

    • select sum(字段名) from 表名;

  • 示例

    • select sum(age) from students;

平均值 avg

  • 格式

    • select avg(字段名) from 表名;

  • 示例

    • select avg(height) from students;

分组 group by

group by + group concat()

  • 格式

    • select 字段名1, group_concat(字段名2 ,字段名3) from 表名 group by 字段名1;

  • 示例

    • select id, group_concat(name, age) from students group by id;

group by + 聚合函数

  • 格式

    • select 字段名1, 函数名(字段名2) from 表名 group by 字段名1;

  • 示例

    • select id, avg(height) from students group by id;

分页 limit

格式

  • select 字段集合 from 表名 limit 起始索引号, 每次显示数量;

示例

  • select * from students limit 3, 5;

连接查询

内连接查询(结果为,两个表共有的数据)

  • 格式

    • select 字段集合 表名1 inner join 表名2 on 表名1.表1字段 = 表名2.表2字段;

  • 示例

    • select * from students inner join classes on students.classes_id = classes.id;

左连接查询(对右表不存在的数据用null填充)

  • 格式

    • select 字段集合 表名1 left join 表名2 on 表名1.表1字段 = 表名2.表2字段;

  • 示例

    • select * from students left join classes on students.classes_id = classes.id;

子查询(一条查询语句中嵌入了另一条查询语句)

  • 示例

    • select * from students where age > (select avg(age) from students);;

小结

查询语句语法顺序

  • select distinct 字段集合
    from 表名
    where 取值范围
    group by 字段名
    order by 字段名
    limit 起始索引号, 每次显示数量

附 创建表sql

create database school_of_three_kindoms charset=utf8;use school_of_three_kindoms;-- 创建学生基本信息表create table students(    -- 学籍号:int unsigned无符号整型, auto_increment自增,primary key设置为主键,not null非空 
    id int unsigned auto_increment primary key not null,    -- 姓名: varchar(30)可变字符类型, default ""默认为空字符
    name varchar(30) default "",    -- 年龄: tinyint unsigned无符号整型, default 0 默认为 0
    age tinyint unsigned default 0,    -- 身高: 浮点型(5个数字,包含2个小数,如 180.05)
    height decimal(5, 2),    -- 性别: enum枚举类型("1"对应"男","2"对应"女","3"对应"保密"")
    gender enum("男","女","保密"),    -- 所属班级: int unsigned 无符号整型,默认值为0
    classes_id int unsigned default 0);-- 创建班级create table classes(    id int unsigned auto_increment primary key not null,    name varchar(20)



);insert into students values
    (null, "曹操", 50, 183.05, 1, 1),
    (null, "夏侯惇", 40, 193.05, 1, 1),
    (null, "许褚", 42, 186.05, 1, 1),
    (null, "司马懿", 48, 188.05, 1, 1),
    (null, "刘备", 48, 179.01, 1, 2),
    (null, "张飞", 46, 179.60, 1, 2),
    (null, "关羽", 47, 188.01, 1, 2),
    (null, "孙权", 39, 185.09, 1, 3),
    (null, "周瑜", 30, 190.09, 1, 3),
    (null, "大乔", 28, 162.32, 2, 3),
    (null, "小乔", 26, 160.19, 2, 3),
    (null, "刑天", 100, 900.15, 1, 4),
    (null, "鬼符三通", 59, 179.68, 1, 5),
    (null, "曹焱兵", 20, 186.34, 1, 5),
    (null, "曹玄亮", 13, 160.21, 1, 5),
    (null, "夏玲", 21, 176.02, 2, 5);insert into classes values
    (0, "班级1_魏"),
    (0, "班级2_蜀"),
    (0, "班级3_吴");

700

Mysql常用查询语法





点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
3867
获赞与收藏
281

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消