MYSQL复习笔记5-select-from-where子句
Date: 20140125
Auth: Jin
参考:http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#select
一、select子句
主要定义需要选取的字段,包括选择selection,投影projection,连接join
(一)选择
1、所有字段
mysql> select * from users;
2、指定字段
mysql> select uname,department,email from users where id>2;
+-------+------------+------------------+
| uname | department | email |
+-------+------------+------------------+
| lily | tech | lily@foxmail.com |
| sum | tech | sum@qq.com |
| jim | market | jim@qq.com |
+-------+------------+------------------+
3、定义字段(列)别名
mysql> select uname as '名字',department as '部门' ,email as '邮箱' from users where id>2;
+--------+--------+------------------+
| 名字 | 部门 | 邮箱 |
+--------+--------+------------------+
| lily | tech | lily@foxmail.com |
| sum | tech | sum@qq.com |
| jim | market | jim@qq.com |
+--------+--------+------------------
4、替换查询结果中的数据
select name,
case
when birthday<'1981' then 'old'
when birthday>'1988' then 'yong'
else 'ok' END YORN
from lee;
SQL Server做法
select id,uname,score=CASE WHEN score < 60 THEN '差' WHEN score >60 and score < 70 then '及格' when score > 70 then '优秀' END
from users;
MYSQL语法
mysql> select id,uname,CASE WHEN score < 60 THEN '差' WHEN score >60 and score < 70 then '及格' when score > 70 and score < 80
then '良好' else '优秀' END as '评级'from users;
+----+--------+--------+
| id | uname | 评级 |
+----+--------+--------+
| 1 | diege | 及格 |
| 2 | hellen | 良好 |
| 3 | lily | 差 |
| 4 | sum | 优秀 |
| 5 | jim | 优秀 |
+----+--------+--------+
5 rows in set (0.01 sec)
5、消除结果集中的重复行 distinct
mysql> select distinct department,uname from users;
6、限制结果集中返回的行数
SQL SERVER top和percent 关键字
MYSQL limit 关键字
mysql> select uname,department from users limit 2;
+--------+------------+
| uname | department |
+--------+------------+
| diege | tech |
| hellen | product |
+--------+------------+
(二)计算,聚合函数
1、计算 + - * %
算术运算
mysql> select 10+2 as '+',10-2 as '-',10*2 as '*',10%2 as '%';
+----+---+----+------+
| + | - | * | % |
+----+---+----+------+
| 12 | 8 | 20 | 0 |
+----+---+----+------+
1 row in set (0.00 sec)
将100分转换为150分值
mysql> select id,uname,score*1.5 as '150分值' from users;
+----+--------+-----------+
| id | uname | 150分值 |
+----+--------+-----------+
| 1 | diege | 99.0 |
| 2 | hellen | 114.0 |
| 3 | lily | 82.5 |
| 4 | sum | 121.5 |
| 5 | jim | 147.0 |
+----+--------+-----------+
2、count 统计总数
mysql> select count(*) from Price;
+----------+
| count(*) |
+----------+
| 9 |
+----------+
1 row in set (0.00 sec)
3、聚合函数 min max sum avg
max
mysql> select max(price) from Price;
+------------+
| max(price) |
+------------+
| 498 |
+------------+
1 row in set (0.00 sec)
min
mysql> select min(price) from Price;
+------------+
| min(price) |
+------------+
| 188 |
+------------+
1 row in set (0.00 sec)
sum
mysql> select sum(price) from Price;
+------------+
| sum(price) |
+------------+
| 2728 |
+------------+
avg
mysql> select avg(price) from Price;
+------------+
| avg(price) |
+------------+
| 303.1111 |
+------------+
(三)字段拼接、文本函数
1、拼接字段 CONCAT
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable ORDER BY full_name;
mysql> SELECT DISTINCT CONCAT('User: ',user,'@',host) AS query FROM mysql.user;
+----------------------------+
| query |
+----------------------------+
| User: root@127.0.0.1 |
| User: @localhost |
| User: root@localhost |
| User: @mnt.localdomain |
| User: root@mnt.localdomain |
+----------------------------+
5 rows in set (0.00 sec)
2、文本函数
Upper() 转大写
Lower() 转小写
Left() 返回左边的字符串
Right() 返回右边的字符串
Length() 返回字符串的长度
其他文本函数可以需要时查询
mysql> select LEFT(pname,3) from Product where pname='T-Shirts1';
+---------------+
| LEFT(pname,3) |
+---------------+
| T-S |
+---------------+
1 row in set (0.00 sec)
mysql> select RIGHT(pname,3) from Product where pname='T-Shirts1';
+----------------+
| RIGHT(pname,3) |
+----------------+
| ts1 |
+----------------+
1 row in set (0.00 sec)
mysql> select Length(pname) from Product where pname='T-Shirts1';
+---------------+
| Length(pname) |
+---------------+
| 9 |
+---------------+
1 row in set (0.00 sec)
(四)获取时间
当前日期 curdate()
当前时间curtime()
当前时间日期 now()
当前unix时间 unix_timestamp()
1、当前日期 curdate()
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2014-01-23 |
+------------+
1 row in set (0.00 sec)
2、当前时间 curdate()
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 21:51:32 |
+-----------+
1 row in set (0.00 sec)
3、当前时间日期 now()
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2014-01-23 21:51:53 |
+---------------------+
1 row in set (0.00 sec)
4、当前unix时间 unix_timestamp()
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1390485160 |
+------------------+
1 row in set (0.00 sec)
可以将时间日期转换为unix时间
mysql> select unix_timestamp('1999-01-20 21:51:30');
+---------------------------------------+
| unix_timestamp('1999-01-20 21:51:30') |
+---------------------------------------+
| 916840290 |
+---------------------------------------+
1 row in set (0.00 sec)
UNIX时间戳转换为日期用函数FROM_UNIXTIME()
mysql> select FROM_UNIXTIME(916840290);
+--------------------------+
| FROM_UNIXTIME(916840290) |
+--------------------------+
| 1999-01-20 21:51:30 |
+--------------------------+
1 row in set (0.00 sec)
二、where子句 数据过滤
使用AND和OR 组合过滤条件
(一)基本操作
返回一条记录
mysql> select * from Product where pname='hat1';
+-----------+-------+------------+-------+
| productId | pname | created | price |
+-----------+-------+------------+-------+
| 1 | hat1 | 2000-11-25 | 100 |
+-----------+-------+------------+-------+
返回多条记录
mysql> select * from Product where created > '2008-11-25';
+-----------+-----------+------------+-------+
| productId | pname | created | price |
+-----------+-----------+------------+-------+
| 4 | hat4 | 2010-08-08 | 218 |
| 18 | T-Shirts4 | 2013-07-15 | 298 |
| 19 | T-Shirts5 | 2009-04-05 | 398 |
+-----------+-----------+------------+-------+
3 rows in set (0.00 sec)
满足多个条件中一个即可
mysql> select * from Product where pname='hat1' or pname='hat2';
+-----------+-------+------------+-------+
| productId | pname | created | price |
+-----------+-------+------------+-------+
| 1 | hat1 | 2000-11-25 | 100 |
| 2 | hat2 | 2003-11-25 | 88 |
+-----------+-------+------------+-------+
2 rows in set (0.01 sec)
同时满足多个条件
mysql> select * from Product where created >'2001-11-25' and price<100;
+-----------+-------+------------+-------+
| productId | pname | created | price |
+-----------+-------+------------+-------+
| 2 | hat2 | 2003-11-25 | 88 |
+-----------+-------+------------+-------+
1 row in set (0.00 sec)
(二) 不匹配操作【取反】 not
mysql> select * from Price where not price >200;
+-----------+-------+
| productId | price |
+-----------+-------+
| 1 | 200 |
| 2 | 188 |
+-----------+-------+
2 rows in set (0.00 sec)
(三)表达式比较
= <> != >= <= > <
mysql> select * from Price where price=200;
+-----------+-------+
| productId | price |
+-----------+-------+
| 1 | 200 |
+-----------+-------+
1 row in set (0.00 sec)
mysql> select * from Price where price!=200;
mysql> select * from Price where price<>200;
+-----------+-------+
| productId | price |
+-----------+-------+
| 2 | 188 |
| 3 | 250 |
| 4 | 318 |
| 15 | 290 |
| 16 | 268 |
| 17 | 318 |
| 18 | 398 |
| 19 | 498 |
+-----------+-------+
8 rows in set (0.00 sec)
mysql> select * from Price where price>=398;
+-----------+-------+
| productId | price |
+-----------+-------+
| 18 | 398 |
| 19 | 498 |
+-----------+-------+
2 rows in set (0.01 sec)
mysql> select * from Price where price>250 and price<300;
+-----------+-------+
| productId | price |
+-----------+-------+
| 15 | 290 |
| 16 | 268 |
+-----------+-------+
2 rows in set (0.00 sec)
(四)模式匹配 like
% 代表0个或多个字符
_ 代表单个字符
[] 指定范围 如[a-f],[0-9]或者集合[abcdef]
[^] 指定不属于的范围 [~a-f],[~0-9]
mysql> select * from Product where pname like 'hat%';
+-----------+-------+------------+-------+
| productId | pname | created | price |
+-----------+-------+------------+-------+
| 1 | hat1 | 2000-11-25 | 100 |
| 2 | hat2 | 2003-11-25 | 88 |
| 3 | hat3 | 2008-06-25 | 150 |
| 4 | hat4 | 2010-08-08 | 218 |
+-----------+-------+------------+-------+
4 rows in set (0.00 sec)
mysql> select * from Product where pname like 'hat_';
+-----------+-------+------------+-------+
| productId | pname | created | price |
+-----------+-------+------------+-------+
| 1 | hat1 | 2000-11-25 | 100 |
| 2 | hat2 | 2003-11-25 | 88 |
| 3 | hat3 | 2008-06-25 | 150 |
| 4 | hat4 | 2010-08-08 | 218 |
+-----------+-------+------------+-------+
4 rows in set (0.00 sec)
不区分大小写
not like
mysql> select * from Product where pname not like 'hat%';
+-----------+-----------+------------+-------+
| productId | pname | created | price |
+-----------+-----------+------------+-------+
| 15 | T-Shirts1 | 2004-06-15 | 190 |
| 16 | T-Shirts2 | 2002-11-10 | 168 |
| 17 | T-Shirts3 | 2000-03-19 | 218 |
| 18 | T-Shirts4 | 2013-07-15 | 298 |
| 19 | T-Shirts5 | 2009-04-05 | 398 |
+-----------+-----------+------------+-------+
[] 指定范围 如[a-f],[0-9]或者集合[abcdef] 【没有成功】
转义%
如果我就真的要查%或者_,怎么办呢?使用escape,转义字符后面的%或_就不作为通配符了,注意前面没有转义字符的%和_仍然起通配符作用
select username from gg_user where username like '%xiao/_%' escape '/';
(五)范围比较 between和in
1、in和not in
mysql> select * from Product where productId in (1,3,19);
+-----------+-----------+------------+-------+
| productId | pname | created | price |
+-----------+-----------+------------+-------+
| 1 | hat1 | 2000-11-25 | 100 |
| 3 | hat3 | 2008-06-25 | 150 |
| 19 | T-Shirts5 | 2009-04-05 | 398 |
+-----------+-----------+------------+-------+
3 rows in set (0.00 sec)
mysql> select * from Product where productId not in (1,3,19);
+-----------+-----------+------------+-------+
| productId | pname | created | price |
+-----------+-----------+------------+-------+
| 2 | hat2 | 2003-11-25 | 88 |
| 4 | hat4 | 2010-08-08 | 218 |
| 15 | T-Shirts1 | 2004-06-15 | 190 |
| 16 | T-Shirts2 | 2002-11-10 | 168 |
| 17 | T-Shirts3 | 2000-03-19 | 218 |
| 18 | T-Shirts4 | 2013-07-15 | 298 |
| 20 | Hat8 | 2000-01-01 | 298 |
+-----------+-----------+------------+-------+
7 rows in set (0.00 sec)
mysql> select * from Product where productId in (select productId from Product where price>300);
+-----------+-----------+------------+-------+
| productId | pname | created | price |
+-----------+-----------+------------+-------+
| 19 | T-Shirts5 | 2009-04-05 | 398 |
+-----------+-----------+------------+-------+
1 row in set (0.00 sec)
in的范围 可以是一个select子句,注意需要()
2、between 和 not between
mysql> select * from users where score between 50 and 70;
+----+-------+------+------------+-------------------+------------+------------+-------+
| id | uname | sex | birthday | email | department | comment | score |
+----+-------+------+------------+-------------------+------------+------------+-------+
| 1 | diege | | 1990-12-31 | diege@foxmail.com | tech | a good boy | 66 |
| 3 | lily | | 1990-12-31 | lily@foxmail.com | tech | a good boy | 55 |
+----+-------+------+------------+-------------------+------------+------------+-------+
2 rows in set (0.00 sec)
mysql> select * from users where score not between 50 and 70;
+----+--------+------+------------+--------------------+------------+---------------+-------+
| id | uname | sex | birthday | email | department | comment | score |
+----+--------+------+------------+--------------------+------------+---------------+-------+
| 2 | hellen | | 1990-12-31 | diege1@foxmail.com | product | a good boy | 76 |
| 4 | sum | | 1980-02-11 | sum@qq.com | tech | a good worker | 81 |
| 5 | jim | | 1985-02-11 | jim@qq.com | market | a good newer | 98 |
+----+--------+------+------------+--------------------+------------+---------------+-------+
(六)空值比较
mysql> select * from users where email is null;
+----+-------+------+----------+-------+------------+---------+-------+
| id | uname | sex | birthday | email | department | comment | score |
+----+-------+------+----------+-------+------------+---------+-------+
| 6 | abing | | NULL | NULL | tech | NULL | 76 |
+----+-------+------+----------+-------+------------+---------+-------+
1 row in set (0.00 sec)
mysql> select * from users where email is not null;
+----+--------+------+------------+--------------------+------------+---------------+-------+
| id | uname | sex | birthday | email | department | comment | score |
+----+--------+------+------------+--------------------+------------+---------------+-------+
| 1 | diege | | 1990-12-31 | diege@foxmail.com | tech | a good boy | 66 |
| 2 | hellen | | 1990-12-31 | diege1@foxmail.com | product | a good boy | 76 |
| 3 | lily | | 1990-12-31 | lily@foxmail.com | tech | a good boy | 55 |
| 4 | sum | | 1980-02-11 | sum@qq.com | tech | a good worker | 81 |
| 5 | jim | | 1985-02-11 | jim@qq.com | market | a good newer | 98 |
+----+--------+------+------------+--------------------+------------+---------------+-------+
注意不是 email not null,而是 email is not null; is关键字还是要的
(七)子查询
mysql> select productId,price from (select * from Price where price>200) as tmp_tb where productId<15;
+-----------+-------+
| productId | price |
+-----------+-------+
| 3 | 250 |
| 4 | 318 |
+-----------+-------+
2 rows in set (0.01 sec)
通过子查询产生一个临时表
三、From子句
select 查询对象由From子句指定
1、单个表或者视图
2、多个表或者视图
mysql> select * from Price,Product;
mysql> select * from Price as a,Product as b where a.productId=b.productId;
3、rowset_fucntion 行集函数
行集函数返回一个表或视图
4、user_define_function 表值函数
5、子查询
mysql> select productId,price from (select * from Price where price>200) as tmp_tb where productId<15;
+-----------+-------+
| productId | price |
+-----------+-------+
| 3 | 250 |
| 4 | 318 |
+-----------+-------+
2 rows in set (0.01 sec)
MYSQL复习笔记5-select-from-where子句的更多相关文章
- MYSQL复习笔记4-基本SQL语句
Date: 20140115Auth: Jin参考:http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#select一:数据库操作1:显示数据 ...
- MYSQL复习笔记9-存储过程
date: 20140208auth: Jin参考引用:http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html mysql存储过程详解一.基本介绍1. ...
- MYSQL复习笔记12-视图
Date: 20140223Auth: Jin参考:http://blog.sina.com.cn/s/blog_436732df0100e768.html 一.介绍1.概念视图是从一个或几个基本表( ...
- MYSQL复习笔记10-连接
Date: 20140219Auth: Jin 一.介绍连接是二元运算,可以对两个表或多个表进行查询.T-SQL中分两大类,一是符合SQL标准的连接谓词表示形式,二是T-SQL扩展的使用关键字JOIN ...
- MYSQL复习笔记8-数据完整性
Date: 20140207Auth: Jin 一.数据完整性的分类数据完整性是指数据库中数据在逻辑上的一致性和准确性.包括三种1.实体完整性又称行的完成性,要求表中有一个主键,其值不能为空且唯一地标 ...
- MYSQL复习笔记7-索引
Date: 20140207Auth: Jin 索引是根据表中一列或若干列按照一定顺序建立的列值与记录行之间的对应关系表. 索引的主要作用 快速存取数据 保证数据记录的唯一性 实现表与表之间的参照完整 ...
- MYSQL复习笔记2-自带工具介绍
Date: 20140102Auth: Jin 一.mysql 命令行客户端1)base-h host-P port--socket=path,-S path用于连接的套接字文件替换使用IP PORT ...
- mysql复习笔记
阅读目录 1.什么是SQL语句2.使用sql语句创建数据库和表3.创建数据表4.数据完整性约束5.四中基本字符类型说明6.SQL基本语句7.类型转换函数8.日期函数9.数学函数10.字符串函数11.联 ...
- MySQL学习笔记:select语句性能优化建议
关于SQL中select性能优化有以下建议,仅当笔记记录. 1.检查索引:where.join部分字段都该加上索引 2.限制工作数据集的大小:利用where字句过滤 3.只选择需要的字段:减少IO开销 ...
随机推荐
- python实战===用python识别图片中的中文
需要安装的模块 PIL pytesseract 需要下载的工具: http://download.csdn.net/download/bo_mask/10196285 因为之前百度云的链接总失效,所以 ...
- 64_a1
AGReader-1.2-16.fc26.x86_64.rpm 13-Feb-2017 23:31 50654 ATpy-0.9.7-11.fc26.noarch.rpm 13-Feb-2017 22 ...
- 【bzoj3224】普通平衡树
看有没有人能发现咯. #include<bits/stdc++.h> #define N 300005 #define rat 4 #define pushup(o) if(o->l ...
- HDU 5129 Yong Zheng's Death
题目链接:HDU-5129 题目大意为给一堆字符串,问由任意两个字符串的前缀子串(注意断句)能组成多少种不同的字符串. 思路是先用总方案数减去重复的方案数. 考虑对于一个字符串S,如图,假设S1,S2 ...
- JS常用操作方法
1.splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 1 <script type="text/javascript"& ...
- MyBatis根据数组、集合查询
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.it ...
- form 表单获取所有数据 封装方法
function getFormJson(frm) { var o = {}; var a = $(frm).serializeArray(); $.each(a, function () { if ...
- memcache和redis的对比
1.memcache a.Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站 ...
- redis之(一)redis的简单介绍
[一]:概念 --->Redis是一个开源的,高性能的,基于键值对的缓存与存储系统 --->Redis数据库中的多有数据都存储在内存中,由于内存的读写速度远快于硬盘,一秒读写超过10万键值 ...
- 基于kbengine 0.4.20
前言: v0.0.1 2015-04-10 誉小痕(shawhen2012@hotmail.com) v0.0.2 2015-04-12 誉小痕(shawhen2012@hotmail.com) ch ...