单表查询

①查询所有     *

mysql> select * from student;

②查询选中字段记录

mysql> select s_name from student;

③条件查询          where

mysql> select s_name from student where s_id<5;

④查询后为字段重命名   as

mysql> select s_name as 名字 from student;

⑤模糊查询     like

%匹配多个字符

mysql> select s_name as 姓名 from student where s_name like '李%';

_匹配一个字符

mysql> select s_name as 姓名 from student where s_name like '李_';

mysql> select s_name as 姓名 from student where s_name like '李__';

⑥排序(默认升序)  order by  以某个字段为主进行排序

升序  asc (asc可以不写)

mysql> select * from student order by sc_id asc;

降序  desc

mysql> select * from student order by sc_id desc;

⑦限制显示数据数量   limit

limit 只接一个数字n时表示显示前面n行

mysql> select * from student limit 5;

limit 接两个数字m,n时表示显示第m行之后的n行

mysql> select * from student limit 2,4;

⑧常用聚合函数

mysql> select * from details;

最大值  max

mysql> select max(age) from details;

最小值 min

mysql> select min(age) from details;

求和 sum

mysql> select sum(age) from details;

平均值 avg

mysql> select avg(age) from details;

四舍五入 round

mysql> select round(avg(age)) from details;

统计  count

mysql> select count(address) from details;

⑨分组查询  group by    筛选条件使用having,having后接条件必须是select后存在的字段

mysql> select age,count(age) from details group by age having age>30;

以age为组统计每个age的人数最后筛选出age大于30的

2、子查询   也叫嵌套查询

mysql> select * from details where age>(select avg(age) from details);

查询所有age大于平均年龄的信息

3、关联查询

①内连接    inner join

无条件内连接  又称笛卡尔连接

mysql> select * from student inner join college;

有条件内连接  在无条件基础上on接条件

mysql> select * from student inner join college on sc_id=c_id;

②外连接

左外连接    left join

以左表为基准,右表没有对应数据以null填充,多余数据去除

mysql> select * from tb1 left join tb2 on id=t_id;

mysql> select * from tb2 left join tb1 on id=t_id;

右外连接   right join

以右表为基准,左表没有对应数据以null填充,多余数据去除

mysql> select * from tb1 right join tb2 on id=t_id;

mysql> select * from tb2 right join tb1 on id=t_id;

派生表必须命名 as

mysql> select * from (select * from details where age>30) as a left join student on d_id=s_id;

---------------------
作者:轻风飞落叶
来源:CNBLOGS
原文:https://www.cnblogs.com/wangwei13631476567/p/8999429.html
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件

[转]MySQL常用查询的更多相关文章

  1. 23个MySQL常用查询语句

    23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...

  2. mysql常用查询归纳

    一.mysql查询的五种子句 where(条件查询).having(筛选).group by(分组).order by(排序).limit(限制结果数) .where常用运算符: 比较运算符 > ...

  3. mysql—常用查询语句总结

    关于MySQL常用的查询语句 一查询数值型数据: ; 查询谓词:>,=,<,<>,!=,!>,!<,=>,=< 二查询字符串 SELECT * FROM ...

  4. MySQL常用查询语句汇总(不定时更新)

    在这篇文章中我会通过一些例子来介绍日常编程中常用的SQL语句   目录: ## 1.数据库的建立     ## 1.数据库的建立   实例将ER图的形式给出:   由此转换的4个关系模式:      ...

  5. mysql常用查询命令

    转引自:https://www.cnblogs.com/widows/p/7137184.html 常用mysql命令 show variables like 'character_set_clien ...

  6. mysql 常用查询

    1.unix时间戳的使用 unix_timesamp.from_unixtime 函数 和 datatime_format函数. // 从datetime 类型取做整形 unixtime时间戳; se ...

  7. MySQL常用查询语句集合《转》

    一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>,= ...

  8. Mysql 常用查询语句

    SELECT * FROM table1 ,,,,,,,,) ) SELECT * FROM table3 WHERE t3Date >= '2011-08-10' SELECT * FROM ...

  9. MySQL常用查询语句积累

    >>MySQL某列插入递增值 SET @i := 100; UPDATE auge_item_classification SET c_code=(@i:=(@i+1)); >> ...

随机推荐

  1. AOP Aspect 统一日志、异常处理、数据格式 【转】

    package com.gsww.chis.aop; import java.util.Arrays; import com.google.common.base.Throwables; import ...

  2. Android——内存管理基础

    内存收集概念 内存垃圾收集器(garbage collector) 概念:自定内存管理. 功能:分配内存.保证所有被引用的对象还在内存中.可以释放在运行的代码中不再引用的对象的内存. 垃圾收集器避免了 ...

  3. Python学习笔记(三)Python基本数字类型及其简单操作(1)

    一.数字类型 表示数字或数值的数据类型称为数字类型,Python语言提供3种数字类型:整数.浮点数和复数,分别对应数学中的整数.实数和复数,下面就一起来了解一下他们吧! 1.整数类型 整数类型与数学中 ...

  4. vue路由history模式刷新页面出现404问题

    vue hash模式下,URL中存在'#',用'history'模式就能解决这个问题.但是history模式会出现刷新页面后,页面出现404.解决的办法是用nginx配置一下.在nginx的配置文件中 ...

  5. MSSQL2008 数据压缩方法

    数据压缩功能使得SOL Server 2008允许在表.索引和分区中执行数据压缩,这样不仅可以节省磁盘空间,而且允许更多数据置入RAM中,从而提升数据库查询的性能. 1.启用行压缩 如果我们要在指定的 ...

  6. goland的下载安装破解并配置

    1.下载地址:https://www.jetbrains.com/go/ 2.安装:简单 3.破解:https://www.cnblogs.com/igoodful/p/9113946.html 4. ...

  7. 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)

    题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...

  8. RocksDB 之Write Ahead Log(WAL)

    Overview RocksDB 中有三个基本的数据结构概念:memtable, sstfile 和 logfile memtable 是个内存数据结构,新写入会插入memtable 切回选择性地写入 ...

  9. bzoj1877 晨跑

    Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他 坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个 ...

  10. JavaScript--缓动动画+轮播图

    上效果: 实现步骤: 最重要的是运动公式!!! <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...