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. Oracle中的4大空值处理函数用法举例

    nvl(exp1,exp2):                           如果exp1为空,则返回exp2:否则返回exp1nvl2(exp1,exp2,exp3):             ...

  2. Java面试16|设计模式

    1.单例模式: 确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下几个要素: 私有的构造方法 指向自己实例的私有静态引用 以自己实例为返回值的静态的公有的方法 单例模式根 ...

  3. Go 语言切片(Slice)

    Go 语言切片是对数组的抽象. Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"),与数组相比切片的长度是不固 ...

  4. Oracle数据库常用命令记录

    1.Sql建表 CREATE TABLE AAABBBCCCDDD( ID ) primary key, AAAAAAAA ) not NULL, BBBBBBBB ), CCCCCCCC ), DD ...

  5. Data access between different DBMS and other txt/csv data source by DB Query Analyzer

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

  6. OpenCV RGB2LAB执行效率测试

    代码 #include <iostream> #include <vector> #include <opencv2/opencv.hpp> #define ERR ...

  7. android Spinner控件详解

    Spinner提供了从一个数据集合中快速选择一项值的办法.默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner ...

  8. Android5.0特性ToolBar

    >Toolbar是什么?大概说一下它的官方介绍.Toolbar是应用的内容的标准工具栏,`可以说是Actionbar的升级版`,两者不是独立关系,要使用Toolbar还是得跟ActionBar扯 ...

  9. Swift基础之init方法,实例方法,类方法(静态方法)的使用(多标签Demo)

    Xcode更新过后,有些方法都进行了改变,Demo中有变化的都进行了简单的标记,具体以后遇见再说 创建一个UIView类,用init方法创建两种类型,显示多标签,创建静态方法进行调用,创建类方法进行调 ...

  10. The Ultimate Guide To iPhone Resolutions

    备忘:http://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutionshttp://appicontemplate.com/