MySQL学习笔记_6_SQL语言的设计与编写(下)
SQL语言的设计与编写(下)
--SELECT查询精讲
概要:
SELECT[ALL | DISTINCT] #distinct
明显的,清楚的,有区别的
{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]} #alias
别名,化名
FROM
表名
[WHERE...]
[GROUPBY...]
[HAVING...]
[ORDERBY ...]
[LIMITcount]
使用SELECT查询语言,目的是可以按用户的想法将数据查出来,将结果返回!
1、字段要列出要查询的字段
e.g. selectname,price from products;
selectprice,name from products;
select* from products;
selectproducts.* from products; #单表其实不需要使用表名
2、可以为每个字段起个别名【后面会用到(关键字,多表查询)】【表也可起别名(多表查询)】
e.g. selectname as bookname,price as bookprice from products;#使用别名;也可不加as;注意别名中有空格时,需要加单引号;
3、使用distinct作用与整个记录,取消重复的数据,只返回一个,而不是单独的一列
e.g. selectdistinct price 'book price' from products;
4、在SQL语句中使用表达式的列(可以使用算术运算符,条件运算符,逻辑运算符...)
e.g. select1+2*3;
select8%5
updateproducts set num = num + 1 where id = 22;
selectname,price,price*0.7 as 'discount price' from products where id <=15;
5、WHERE可以在SELECT/UPDATE/DELETE中
a)可使用的逻辑运算符号(将多个条件组合)
&&/AND ||/OR !/NOT
b)可使用的比较运算符号
=#判断是否相等,与程序中的==作用相同
<=>#判断是否相等,与=一致,但可以用于与NULL比较
!=/ <> #不等号
<
<=
>
>=
c)程序中没有的运算符
ISNULL #与'<=>NULL'
相等
ISNOT NULL
BETWEENAND
e.g. select* from products where id between 10 and 20;
与 “select* from products where id >= 10 && id <= 20;”作用相同
NOTBETWEEN AND
IN
e.g. select* from products where id in(5,10,15,20);
updateproducts set num = 77 where id in(5,10,15,20);
deletefrom products where id in(5,10);
d)模糊查询
LIKE _(任意一个字符)和%(0个或多个任意字符)两个通配符号
e.g. select* from products where name like '______'; #查找任意名字为6个字符的数据
select* from products where name like '%java%'; #查询名字中包含有java的数据
NOTLIKE
e.g. select* from products where name not like '%java%'; #查询名字中不包含java字样的数据。
REGEXP/RLIKE【正则表达式】
#RegExp 正则表达式
e.g. select* from products where name regexp '^java'; #查找所有以java开头的数据
select* from products where name regexp 's$'; #查找所有以s结尾的数据
6、多表查询(连接查询),比较常用
#ambiguous
e.g. selectcats.name,products.name from cats,products;
selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p;#将A表中的记录与B表中的记录依次匹配,得到A*B种结果【笛卡尔乘积】,该结果是没有意义的。
selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid;
selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid and c.id=3;
selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,catsb; #将单表分为多表,进行查询
selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,cats b wherea.pid = b.id;
7、嵌套查询子查询
e.g. select* from products where cid in(select id from carts where name regexp'^j') ;
select* from products where cid in(select id from carts where name like'j%'); #作用相同
8、orderby
字段 [asc正序]desc倒序
e.g. select* from order by name;
select* from order by price; #按价格非递减排序
select* from order by price desc; #非递增排序
select* from where cid > 5 order by price desc; #与where结合使用
9、limitcount【限制显示个数】
e.g. select* from limit 7;
select* from order by id desc limit 7;
select* from where id < 10 order by id desc limit 7;
select* from where id > 14 order by id asc limit 0,1; #limit0,1表示从第0个开始取,取1个
10、groupby
字段【分组】
常用函数:
count() #一个字段的总数
sum()
avg()#平均值
max()
min()
e.g. selectcount(*),sum(price),avg(price),max(price),min(price) from products;
selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid;
selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid having avg(price) > 50; #加having条件,与where类似
#having必须与gropby结合才能使用
MySQL学习笔记_6_SQL语言的设计与编写(下)的更多相关文章
- MySQL学习笔记_5_SQL语言的设计与编写(上)
SQL语言的设计与编写(上) 一.SQL语句分类 数据定义语言(DDL): 用于定义和管理数据对象,包括数据库.数据表.视图.索引等.例如:CREATE.DROP.ALTER等语句. 数据操作语言(D ...
- MySQL学习笔记_8_SQL语言基础复习
SQL语言基础复习 一.概述 SQL语句注释方式 1)以"#"开头直到行尾的所有内容都是注释 2)以"--"(--后还有一个空格)开头直到行尾的所有内容都是注释 ...
- MySQL学习笔记一
MySQL 学习笔记 一 一.数据库简单介绍 1. 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: Ø 网状型数据库 Ø 层次型数据库 Ø 关系型数据库 Ø 面向对象数据库 上面4中数据库系 ...
- MySQL学习笔记-锁相关话题
在事务相关话题中,已经提到事务隔离性依靠锁机制实现的.在本篇中围绕着InnoDB与MyISAM锁机制的不同展开,进而描述锁的实现方式,多种锁的概念,以及死锁产生的原因. Mysql常用存储引擎的锁 ...
- MySQL学习笔记-cache 与 buffer
Cache和Buffer是两个不同的概念,简单的说,Cache是加速"读",而 buffer是缓冲"写",前者解决读的问题,保存从磁盘上读出的数据,后者是解决写 ...
- MySQL学习笔记-大纲
软件程序性能测试在之前<品味性能之道>系列中已经大量提到,讲解了很多测试方法.测试观念.测试思想等等.最近准备深入MySQL进行学习并总结.分别查阅<MySQL性能调优与架构设计&g ...
- 数据库MySQL学习笔记高级篇
数据库MySQL学习笔记高级篇 写在前面 学习链接:数据库 MySQL 视频教程全集 1. mysql的架构介绍 mysql简介 概述 高级Mysql 完整的mysql优化需要很深的功底,大公司甚至有 ...
- MySql学习笔记三
MySql学习笔记三 4.DML(数据操作语言) 插入:insert 修改:update 删除:delete 4.1.插入语句 语法: insert into 表名 (列名1,列名2,...) val ...
- 一千行MySQL学习笔记 (转)
出处: 一千行MySQL学习笔记 /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权 ...
随机推荐
- go优化
1.安装graphviz软件,安装步骤如下::(1)安装graphvizyum install 'graphviz*' (2)查看已安装graphviz包yum list 'graphviz*' 2. ...
- python笔记四(dict/set/不可变对象)
一.dict 字典是包含key_value存储方式.在放进去的时候,必须根据key值Hash出value的存放位置,这样,取的时候才能根据key直接拿到value. dict的操作: d = {'Mi ...
- Java第7次实验提纲(多线程)
PTA与参考资料 题集:多线程 多线程实验参考文件 ThreadReading 实验-基础部分 1.1 基础题目MyThread类.自行完成题集合的:PrintTask 1.2 Runnable与匿名 ...
- 准备在CSDN知识库建立一个Ext JS的知识库
CSDN近期正在建立一个知识库,目标是打造身边的技术百科全书 ,我觉得这创意挺好,就像stackoverflow一样,常见的问题在里面基本都有了,只要通过搜索就能找到所需的答案. 现在,大家对于Ext ...
- Java程序员的Golang入门指南(上)
Java程序员的Golang入门指南 1.序言 Golang作为一门出身名门望族的编程语言新星,像豆瓣的Redis平台Codis.类Evernote的云笔记leanote等. 1.1 为什么要学习 如 ...
- 这是最好的时光,这是最坏的时光 SNAPSHOT
好久没动笔了,上次憋了好几天码出的文字扔出去,石沉大海,没惊起半点涟漪.这次真不知道能憋出个什么鬼,索性就让思绪飞扬,飞到哪是哪! --题记 此处应有BGM: 少年锦时 赵雷 1.以后真没有暑假喽 2 ...
- android M Launcher之数据库实现
前面一系列文章我们分析了LauncherModel的工作过程,它会把数据绑定到桌面上.从今天开始我们来分析下Launcher的数据来源即Launcher数据库的实现. 一个完整的数据库实现都应该包括两 ...
- Scikit-learn:模型评估Model evaluation 之绘图
http://blog.csdn.net/pipisorry/article/details/53001866 绘制ROC曲线 def plotRUC(yt, ys, title=None): ''' ...
- Detailed Item Cost Report (XML) timed out waiting for the Output Post-processor to finish
In this Document Symptoms Cause Solution References APPLIES TO: Oracle Cost Management - Ver ...
- Android app内存管理的16点建议
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiopshared memory(共享内存) Android通过下面几个方式在不同的Process中来共享RAM: 每一个app的proc ...