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语言的设计与编写(下)的更多相关文章

  1. MySQL学习笔记_5_SQL语言的设计与编写(上)

    SQL语言的设计与编写(上) 一.SQL语句分类 数据定义语言(DDL): 用于定义和管理数据对象,包括数据库.数据表.视图.索引等.例如:CREATE.DROP.ALTER等语句. 数据操作语言(D ...

  2. MySQL学习笔记_8_SQL语言基础复习

    SQL语言基础复习 一.概述 SQL语句注释方式 1)以"#"开头直到行尾的所有内容都是注释 2)以"--"(--后还有一个空格)开头直到行尾的所有内容都是注释 ...

  3. MySQL学习笔记一

    MySQL 学习笔记 一 一.数据库简单介绍 1. 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: Ø 网状型数据库 Ø 层次型数据库 Ø 关系型数据库 Ø 面向对象数据库 上面4中数据库系 ...

  4. MySQL学习笔记-锁相关话题

    在事务相关话题中,已经提到事务隔离性依靠锁机制实现的.在本篇中围绕着InnoDB与MyISAM锁机制的不同展开,进而描述锁的实现方式,多种锁的概念,以及死锁产生的原因.   Mysql常用存储引擎的锁 ...

  5. MySQL学习笔记-cache 与 buffer

    Cache和Buffer是两个不同的概念,简单的说,Cache是加速"读",而 buffer是缓冲"写",前者解决读的问题,保存从磁盘上读出的数据,后者是解决写 ...

  6. MySQL学习笔记-大纲

    软件程序性能测试在之前<品味性能之道>系列中已经大量提到,讲解了很多测试方法.测试观念.测试思想等等.最近准备深入MySQL进行学习并总结.分别查阅<MySQL性能调优与架构设计&g ...

  7. 数据库MySQL学习笔记高级篇

    数据库MySQL学习笔记高级篇 写在前面 学习链接:数据库 MySQL 视频教程全集 1. mysql的架构介绍 mysql简介 概述 高级Mysql 完整的mysql优化需要很深的功底,大公司甚至有 ...

  8. MySql学习笔记三

    MySql学习笔记三 4.DML(数据操作语言) 插入:insert 修改:update 删除:delete 4.1.插入语句 语法: insert into 表名 (列名1,列名2,...) val ...

  9. 一千行MySQL学习笔记 (转)

    出处:  一千行MySQL学习笔记 /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权 ...

随机推荐

  1. DotnetSpider (一) 架构的理解、应用、搭建

    第一次写博客,比较浅显,欢迎大牛们指点一二,不胜感激.   ** 温馨提示:如需转载本文,请注明内容出处.**   本文连接:http://www.cnblogs.com/grom/p/8931650 ...

  2. Jmeter(三)_配置元件

    HTTP Cookie Manager 用来存储浏览器产生的用户信息 Clear Cookies each Iteration:每次迭代请求,清空cookies,GUI中定义的任何cookie都不会被 ...

  3. ACM 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品, ...

  4. JS基础速成(二)-BOM(浏览器对象模型)

    .t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.BOM(浏览器对象模型) 1.screen对象. console.lo ...

  5. Bootstrap3 表单-被支持的控件:输入框

    输入框包括大部分表单控件.文本输入域控件,还支持所有 HTML5 类型的输入控件: text.password.datetime.datetime-local.date.month.time.week ...

  6. Apache ActiveMQ实战(1)-基本安装配置与消息类型

    ActiveMQ简介 ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信.ActiveMQ使用Apache ...

  7. 万众瞩目之下,ANGULAR 2终于正式发布啦!

    转载:https://angular.io/ 怀着期盼的心情,终于盼到了稳定版本,那么我就可以专心研究了,不再为不定期的修复烦恼咯. 今天,在 Google 总部一个特别的聚会上,我们发布了 Angu ...

  8. Markdown语法及SublimeText下使用技巧

    Markdown语法及SublimeText下使用技巧 0.缘起 最近因为一直在学习Sublime Text,所以也就顺便试用了一下ST对Markdown的支持.正好CSDN正在大力宣传新上线的Mar ...

  9. The new powerful SQL executing schedule monthly or weekly in DB Query Analyzer 7.01

    1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. ...

  10. Low-rank approximations

    Low-rank approximations Give matrix and a positive integer , we wish to find an matrix of rank at mo ...