单表查询

①查询所有     *

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. vue使用flexible和px2rem实现移动端适配

    首先下载flexible.js和px2rem npm install px2rem-loader 对webpack进行配置.进入build文件夹对utils.js中的postcssLoader做如下修 ...

  2. day38 19-Spring整合web开发

    整合Spring开发环境只需要引入spring-web-3.2.0.RELEASE.jar这个jar包就可以了,因为它已经帮我们做好了. Spring整合web开发,不用每次都加载Spring环境了. ...

  3. qt获取本机用户名

    //获取用户名 QString getUserName() { #if 1 QStringList envVariables; envVariables << "USERNAME ...

  4. js图片裁切

    js的图片裁切只支持移动和右下拉 html部分 <div id="box" class="box"> <img class="img ...

  5. app被Rejected 的各种原因翻译。这个绝对有用

    1. Terms and conditions(法律与条款) 1.1  As a developer of applications for the App Store you are bound b ...

  6. Python发送邮件1(带附件的)

    普通的发邮件(不使用类)

  7. hdu5438 dfs+并查集 长春网赛

    先dfs对度小于2的删边,知道不能删为止. 然后通过并查集来计算每一个分量里面几个元素. #include<iostream> #include<cstring> #inclu ...

  8. laravel 博客项目部署到Linux系统后报错 权限都设置为777,仍然报错没有权限

    阿里工程师最后给出解决方案.

  9. React Native开源项目如何运行(附一波开源项目)

    学习任何技术,最快捷的方法就是学习完基础语法,然后模仿开源项目进行学习,React Native也不例外.React Native推出了1年多了, 开源项目太多了,我们以其中一个举例子.给大家演示下如 ...

  10. Java练习 SDUT-2240_织女的红线

    织女的红线 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 好久不见牛郎哥哥了,织女非常想他,但是她想考验一下牛郎在她不 ...