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)以"--"(--后还有一个空格)开头直到行尾的所有内容都是注释 ...
- C语言程序设计学习笔记:P1-程序设计与C语言
本系列博客用于记录学习浙江大学翁恺老师的C语言程序设计,系列笔记链接如下: C语言程序设计学习笔记:P1-程序设计与C语言 C语言程序设计学习笔记:P2-计算 C语言程序设计学习笔记:P3-判断 C语 ...
- MySQL学习笔记8:权限管理、数据库备份与设计
1.前言 学习视频源自:[狂神说Java]MySQL最新教程通俗易懂 B站视频链接:https://www.bilibili.com/video/BV1NJ411J79W [狂神说Java]MySQL ...
- MySQL学习笔记02【SQL基本概念与通用语法、数据库的CRUD操作】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- MySQL学习笔记01【数据库概念、MySQL安装与使用】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- MySQL学习笔记-基础篇1
MySQL 学习笔记–基础篇1 目录 MySQL 学习笔记--基础篇1 1. 数据库概述与MySQL安装 1.1 数据库概述 1.1.1 为什么要使用数据库 1.2 数据库与数据库管理系统 1.2.1 ...
- MySQL学习笔记-恶补基础篇
目录 概述 1.初识数据库 1.1.DataBase 1.2.数据库分类 1.3.DBMS(数据库管理系统) 1.4.命令行操作数据库 2.操作数据库 2.1.操作数据库 2.2.数据库列类型 2.3 ...
- MySQL学习笔记(三)查询
写在前面:本篇为作者自学总结,学习内容为课堂所学和网络学习笔记汇总,对于内容引用部分在文中和文末注明. 文章仅供参考,如需深入了解,请查阅MySQL参考手册.附上下载链接: 链接:https://pa ...
最新文章
- 用Python解“求整数段和”题
- STC8K28步进电机ULC2003触发器
- $dbms=mysql_MySQL学习笔记(一)
- 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化
- android 上下滚动文字_计算机毕设项目004之Android系统在线小说阅读器
- linux 普通用户touch权限不够_Linux 开启指令
- oracle 超市管理系统,SuperManager 超市账单管理系统
JSP + Servlet + Oracle Jsp/ 240万源代码下载- www.pudn.com...
- 再谈网络字节顺序,大小端问题
- 晨哥真有料丨到底合适重要,还是感觉重要!
- 【时间序列预测】基于matlab麻雀算法优化LSTM时间序列预测【含Matlab源码 JQ001期】
- 由陌生到认识——物联网LoRa技术入门简介
- 【软件测试】测试用例
- 2、ARM嵌入式系统:LED初始化
- 年度编程榜出炉!Python第一,Java程序员哭晕:又翻车了!
- 蓝色主题登陆页面界面模板
- 电子线路(线性部分)——第一章 晶体二极管
- 罗格斯大学电子与计算机工程,罗格斯大学电子和计算机工程理学硕士研究生申请要求及申请材料要求清单...
- 回路、简单回路、简单路径
- [Excel 与 股票] 一图胜千言之 Excel 处理股票数据
- 重生之丹王,炼丹入门