<导图>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 表名 where 字段名 between 点值 and 点值;
    • 示例

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

    • 格式

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

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

数据排序

正序 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_吴");
  
Mysql常用查询语法
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容