SELECT查询语句

  ---进行单条记录、多条记录、单表、多表、子查询……

SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[MAX_STATEMENT_TIME = N]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]

select书写使用技巧:

  ①确认需要访问数据来自哪几张表

    from来自某张表或者某几张表

    join添加某张表

    on表连接条件

  记住一点:每关联一个表就需要加上对应的on条件(on条件就是主外键条件)

  ②通过where条件过滤数据

  ③确认需求里面是否有分组聚合的含义

    分组:group by

    聚合:聚合函数

    聚合条件过滤:having

  ④是否需要排序

    order by

1、查询某张表所有数据

mysql> select * from temp;

解析:*代表所有列,temp代表表名,不带条件就查询所有数据

2、查询指定列和条件的数据

mysql> select name,age from temp where age = ;

解析:查询name和age这两列,age 等于22的数据。

3、对查询的数据进行运算操作

mysql> select age+,age/,age-,age* from temp where age- > ;

mysql> select PLAYRNO,AMOUNT,AMOUNT*6.5 “Ren Min Bi” from PENALTIES;

解析:查询AMOUNT列数据乘6.5并改列名为Ren Min Bi

使用小括号可以改变运算的优先级

4、concat函数,字符串连接

mysql> select NAME,concat(TOWN,STREET,HOUSENO) “player Home Address” from PLAYERS;

解析:利用concat函数将TOWN,STREET,HOUSENO三列的字符串连接起来,别名列为player Home Address(可以在concat里加' '进行分隔)

注意:concat和null进行连接,会导致连接后的数据成为null

mysql> select * from t1;
+------+--------+--------+
| id | First | Last |
+------+--------+--------+
| | zhang | jiacai |
| | linghu | NULL |
+------+--------+--------+ mysql> select id,concat(First,' ',Last) from t1;
+------+------------------------+
| id | concat(First,' ',Last) |
+------+------------------------+
| | zhang jiacai |
| | NULL |
+------+------------------------+

concat_ws函数,指定分隔符的字符串连接

mysql> select id,concat_ws(':',First,Last) "Full Name" from t1;
+------+--------------+
| id | Full Name |
+------+--------------+
| | zhang:jiacai |
| | linghu |
+------+--------------+ 圆括号里的第一个位置用来指定字符串连接的分隔符

5、as 列别名(或省略,留空格)

mysql> select id as 'num' from t1;

mysql> select id 'num',First from t1;

6、distinct关键字去掉重复数据

mysql> select * from t2;
+------+
| num |
+------+
| |
| |
| |
| |
+------+ mysql> select distinct num from t2;
+------+
| num |
+------+
| |
| |
+------+ distinct 多列:去重的是同行多列组合的重复数据 mysql> select distinct id, age from temp;

7、where条件查询

where语句中的条件比较:大于>、大于等于>=、小于<、小于等于<=、等于=、不等于<>

mysql> select * from tableName where a> or a>= or a< or a<= or a= or a<>;

8、and 并且、or 或者、not非

mysql> select * from temp where age> and name=‘jack’;

解析:查询name等于jack并且年龄大于20的

mysql> select * from tmep where name=‘jack’ or name=‘jackson’;

解析:查询name是jack或是jackson的

mysql> select * from temp where not (age > );

解析:取小于等于20的数据

mysql> select * from temp where id not in(, );

解析:查询id数不是1,也不是2的

9、between v1 and v2:v1和v2之间

mysql> select * form temp where age between  and ;

解析:查询age在20和25之间的

10、in 查询:多个条件 类似于or

mysql> select * from temp where id in (, , );

解析:查询id在括号中出现的数据

11、like 模糊查询

%:替代0个或多个字符

_:替代一个字符

mysql> select * from temp where name like ‘j%’;

解析:查询name以j开头的(%通配所有)

mysql> select * from temp where name like ‘%k%’;

解析:查询name包含k的

escape转义

mysql> select * from temp where name like ‘\_%’ escape ‘\’;

解析:指定\为转义字符,上面的就可以查询name中以“_”开头的数据

12、is null、is not null

mysql> select * from temp where name is null;

解析:查询为null的数据

mysql> select * from temp where name is not null;

解析:查询不为null的数据

13、order by排序:desc降序、asc升序

mysql> select * from temp order by id;  (默认asc升排序)

mysql> select * from temp order by id desc;  (指定降序排)

多列组合

mysql> select * from temp order by id, age;

14、limit子句:从结果集中选取最前面或最后面的几行

  通常和order by连用,放其后面

limit  <获取的行数> [OFFSET <跳过的行数>]

limit [<跳过的行数>,] <获取的行数>

mysql> select playerno,name from players order by playerno asc limit ,;
mysql> select playerno,name from players order by playerno asc limit offset ;
解析:跳过前面的3行,从第4行开始取,取5行

注意:MySQL5.7 doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

15、group by、having 分组聚合

select [聚合函数] 字段名 from 表名

  [where 查询条件]

  [group by 字段名]

  [having 过滤条件]

mysql> select salary,count(*) from salary_tab
-> where salary>=
-> group by salary
-> having count(*)>=;
+---------+----------+
| salary | count(*) |
+---------+----------+
| 2000.00 | |
| 3000.00 | |
+---------+----------+

##########欲知详细解析,请听下回分解##########

基础SELECT实例的更多相关文章

  1. 基础 jQuery 实例

    基础 jQuery 实例 jQuery 原则: 由于 jQuery 是为处理 HTML 事件而特别设计的,那么当您遵循以下原则时,您的代码会更恰当且更易维护: 把所有 jQuery 代码置于事件处理函 ...

  2. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  3. AutoCAD ObjectARX(VC)开发基础与实例教程2014版光盘镜像

    AutoCAD ObjectARX(VC)开发基础与实例教程2014,最新版,光盘镜像 作者:张帆 朱文俊 编著 出版社:中国电力出版社 出版时间:2014年6月 点击一下

  4. SQL基础用法(实例二)

    /* 2006年10月01日 SQL Server 数据库的高级操作 (1) 批处理 (2) 变量 (3) 逻辑控制 (4) 视图 (5) 函数 (6) 高级查询 */ ()批处理 将多条SQL语句作 ...

  5. expect基础及实例

    expect基础及实例 http://blog.csdn.net/zhuying_linux/article/details/6900805

  6. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

    花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用oracle用 ...

  7. SVG基础绘图实例

    SVG可缩放矢量图(Scalable Vector Graphics),是使用 XML 来描述二维图形和绘图程序的语言,图像在放大或改变尺寸的情况下其图形质量不会有所损失,是万维网联盟的标准. 下面整 ...

  8. jQuery基础与实例

    一.简介 1.什么是jQuery jQuery是一个轻量级.快速简洁的javaScript库,能让我们方便快捷的选择元素操作元素属性. 2.下载地址 3.jQuery使用方式 $("div& ...

  9. Vue.js 基础指令实例讲解(各种数据绑定、表单渲染大总结)——新手入门、高手进阶

    Vue.js 是一套构建用户界面的渐进式框架.他自身不是一个全能框架--只聚焦于视图层.因此它非常容易学习,非常容易与其它库或已有项目整合.在与相关工具和支持库一起使用时,Vue.js 也能完美地驱动 ...

随机推荐

  1. HTML select 选中触发事件

    $(function () { $("#cityidchange").change(function (data) { var cityid = $("#cityidch ...

  2. python datetime unix时间戳以及字符串时间戳转换

    将python的datetime转换为unix时间戳 import time import datetime dtime = datetime.datetime.now() ans_time = ti ...

  3. 引入css少引入了rel="stylesheet" 这行代码。导致整个页面空白、

    忘记引入rel="stylesheet" 如下: <link  href="css/swiper-3.3.1.min.css" /> 这样使得整个页 ...

  4. 扫描二维码或其他操作情况下返回界面,onActivityResult()不执行的问题

    在使用第三方zxing扫描时,部分手机(好像都是4.4及以下版本的手机)扫描后不调用onActivityResult()返回结果. 调试发现zxing的扫描界面CaptureActivity 在注册时 ...

  5. In R, how to split/subset a data frame by factors in one column?

    按照某列的值拆分data.frame My data is like this (for example): ID Rate State 1 24 AL 2 35 MN 3 46 FL 4 34 AL ...

  6. 简单MVVM教程

    WPF+MVVM的本地桌面程序为背景,这样一来我们可以不去操心服务器那部分的事情,更加专注我们的MVVM.我打算把最重要的部分放到开头来讲,而接下来这最重要的部分却是全篇教程唯一没有代码的部分.好,下 ...

  7. php的几种算法(转载)

    <? //-------------------- // 基本数据结构算法 //-------------------- //二分查找(数组里查找某个元素) function bin_sch($ ...

  8. Mac eclipse 快捷键 f6、f8 失效

    解决方法:

  9. (原)使用android studio ndk开发流程

    先使用android stuido创建一个app工程,创建工程的时候,.gradle目录结构下为2.8目录.(note:2.10目录为后续更新结果出现.) 依次修改上述红色方框标注部分内容: 1)修改 ...

  10. SQL Server 定时执行SQL语句的方法

    SQL SERVER 定时任务,你可以启动一下.不过要想更加直观的控制,直接写一个程序,定时执行你的存储过程. 1.设置“SQL Server 代理”(SQL Server Agent)服务随系统启动 ...