网站建设公司宣传语,WordPress仿什么值得买,帮客户做网站挣钱吗,安阳百度概念#xff1a;
1#xff0c;数据库管理系统一个重要功能就是数据查询#xff0c;数据查询不应只是简单返回数据库中存储的数据#xff0c;还应该根据需要对数据进行筛选以及确定数据以什么样的格式显示
2#xff0c;MySQL提供了功能强大、灵活的语句来实现这些操作
3…概念
1数据库管理系统一个重要功能就是数据查询数据查询不应只是简单返回数据库中存储的数据还应该根据需要对数据进行筛选以及确定数据以什么样的格式显示
2MySQL提供了功能强大、灵活的语句来实现这些操作
3MySQL数据库使用select语句来查询数据
查询格式
select[all|distinct]目标列的表达式1[别名]目标列的表达式2[别名]...
from 表名或视图名 [别名],表名或视图名 [别名]...
[where条件表达式]
[group by列名]
[having条件表达式]
[order by 列名[asc|desc]]
[limit数字或者列表];
简化版语法
select *|列名 from 表 where 条件
1查询
1数据准备
创建数据库和表 添加数据 2简单查询
select pid,pname,price,category_id from product;
select * from product;--查询所有商品select pname,price from product;--查询商品名和商品价格--别名查询使用的关键字是as(as可以省略)
--1,表别名
select *from product as p;
select *from product p;--2,列别名
select pname as 商品名,price 商品价格 from product;--3,去掉重复值
select distinct price from product;
select distinct * from product;--去除所有列都一样的--5,查询结果是表达式将所有商品都加价10元进行显示
select pname,price10 new_price from product;
2运算符
简介
数据库中的表结构确立后表中的数据代表的意义就已经确定。通过MySQL运算符进行运算就可以获取到表结构以外的另一种数据
例如学生表中存在一个bith字段这个字段表示学生的出生年份。而运用MySQL的算术运算符用当前的年份减学生出生的年份那么得到的就是这个学生的实际年龄数据
MySQL支持4种运算符
算术运算符比较运算符逻辑运算符位运算符
1算数运算符
算术运算符说明加法运算-减法运算*乘法运算/或 DIV除法运算返回商%或 MOD求余运算返回余数
2比较运算符
比较运算符说明等于和小于和小于等于和大于和大于等于安全的等于两个操作码均为NULL时其所得值为1;而当一个操作码为NULL时其所得值为0或!不等于IS NULL 或 ISNULL判断一个值是否为NULLIS NOT NULL判断一个值是否不为NULLLEAST当有两个或多个参数时返回最小值GREATEST当有两个或多个参数时返回最大值BETWEEN AND判断—个值是否落在两个值之间IN判断一个值是IN列表中的任意一个值NOT IN判断一个值不是IN列表中的任意一个值LIKE通配符匹配REGEXP正则表达式匹配
3逻辑运算符
逻辑运算符说明NOT或者!逻辑非AND或者逻辑与OR或者 ||逻辑或XOR逻辑异或
4位运算符
位运算符说明|按位或按位与^按位异或按位左移按位右移~按位取反反转所有比特
位运算符是在二进制数上进行计算的运算符。位运算会先将操作数变成二进制数进行位运算。然后再将计算结果从二进制数变回十进制数
3运算符操作
1算术运算符
select 62;
select 6-2;
select 6*2;
select 6/2;
select 6%2;select pname,price10 new_price from product;select pname,price*1.1 new_price from product;
2比较运算符
3逻辑运算符
select pname,price10 new_price from product;select pname,price*1.1 new_price from product;--查询海尔洗衣机商品的所有信息
select * from product where pname海尔洗衣机;--查询价格为800的商品
select *from product where price800;--查询价格不是800的所有商品
select *from product where price!800;
select *from product where price800;
select *from product where not (price800);--查询价格大于等于60的所有商品
select *from product where price60;--查询价格在200到1000的所有商品
select *from product where price between 200 and 1000;
select *from product where price200 and price1000;
select *from product where price200 price1000;--查询价格是200或者1000的所有商品
select *from product where price in(200,800);
select *from product where price200 or price1000;
select *from product where price200 || price1000;--插叙包含‘裤’字所有的商品
select *from product where pname like 裤;
select *from product where pname like %裤;
select *from product where pname like %裤;--用来匹配任意字符--查询以‘海’字开头的商品
select *from product where pname like 海%;--查询第二个字为‘蔻’的所有商品
select *from product where pname like _蔻%;--下划线匹配单个字符--查询category_id为null的商品
select *from product where category_id is null;
--查询category_id不为null的商品
select *from product where category_id is not null;--使用least求最小值
select least (10,2,20) as small_number;
select least (10,null,20) as small_number;--如果求最小值时有一个为null不会进行比较结果直接为null--使用greatest求最大值
select greatest (10,20,30) as big_number;
select greatest (10,null,20) as big_number;--null
4位运算符
位运算符是在二进制数上进行计算的运算符。位运算会先将操作数变成二进制数进行位运算。然后再将计算结果从二进制数变回十进制数
select 35; --位与 10011 0101----0001 --都为1则为1
select 3|5;--位或 70011 0101----0111 --有一个为1就为1
select 3^5;--位异或 60011 0101----0110 --相同为0不同为1
select 31;--位右移 10011 1-----0001
select 31;--位左移 60011 6-----0110select ~3;--位取反 18446744073709551612000000000000000000011-1111111111111111111100 --0变11变0
4排序查询
介绍
如果我们需要对读取的数据进行排序我们就可以使用MySQL的order by子句来设定你想按哪个字段哪种方式来进行排序再返回搜索结果
select字段名1字段名2.....
from表名
order by字段名1 [asc|desc]字段名2[asc|desc].....
特点
1.asc代表升序desc代表降序如果不写默认升序
2.order by用于子句中可以支持单个字段多个字段表达式函数别名
3.order by子句放在查询语句的最后面。LIMIT子句除外
select * from product order by price ;select * from product order by price desc;select * from product order by price desc,category_id desc;select distinct price from product order by price desc;--去重并降序
5聚合查询
简介
之前我们做的查询都是横向查询它们都是根据条件一行一行的进行判断而使用聚合函数查询是纵向查询它是对一列的值进行计算然后返回一个单一的值;另外聚合函数会忽略空值
聚合函数作用count()统计指定列不为NULL的记录行数;sum()计算指定列的数值和如果指定列类型不是数值类型那么计算结果为0max()计算指定列的最大值如果指定列是字符串类型那么使用字符串排序运算;min()计算指定列的最小值如果指定列是字符串类型那么使用字符串排序运算;avg()计算指定列的平均值如果指定列类型不是数值类型那么计算结果为o
select count(pid) from product;--商品总条目select count(pid) from product where price 200;--商品大于200select sum(price) from product where category_idc001;--为c001商品的总和select max(price) from product;--商品最大价格select min(price) from product;--商品最小价格select max(price),min(price) from product;select avg(price) from product where category_idc002;--c002的平均价格
1聚合查询-NULL值的处理
1、count函数对null值的处理
如果count函数的参数为星号(*)则统计所有记录的个数。而如果参数为某字段不统计含null值的记录个数
2、sum和avg函数对null值的处理
这两个函数忽略null值的存在就好象该条记录不存在一样
3、max和min函数对null值的处理
max和min两个函数同样忽略null值的存在
6分组查询
1group by
简介
分组查询是指使用group by字句对查询信息进行分组
格式
select 字段1字段2.... from 表名 group by 分组字段 having 分组条件;
如果要进行分组的话则SELECT子句之后只能出现分组的字段和统计函数其他的字段不能出现
注意分组之后select的后边只能写分组字段和聚合函数
select category_id,count(pid) from product group by category_id;--统计各个分类商品的个数
2分组之后的条件筛选-having
分组之后对统计结果进行筛选的话必须使用having不能使用where
where子句用来筛选FROM子句中指定的操作所产生的行
group by 子句用来分组WHERE子句的输出
having子句用来从分组的结果中筛选行
格式
select 字段1,字段2... from 表名 group by 分组字段 having 分组条件;
--分类显示大于4的商品信息
--SQL执行顺序:from - group by - count(pid) - select - having -order by
select category_id,count(pid) as cnt from product group by category_id having count(pid)4 order by cnt;
7分页查询
简介
分页查询在项目开发中常见由于数据量很大显示屏长度有限因此对数据需要采取分页显示方式。例如数据共有30条每页显示5条第一页显示1-5条第二页显示6-10条
格式
--方式1-显示前n条
select 字段1字段2... from 表明 limit n
--方式2-分页显示
select 字段1字段2... from 表名 limit m,n
m:整数表示从第几条索引开始计算方式当前页-1)*每页显示条数
n:整数表示查询多少条数据
select * from product limit 5;--查询product表的前5条记录select * from product limit 3,5;--从第4条开始显示5条
8insert into select语句
简介
将一张表的数据导入到另一张表中可以使用INSERT INTO SELECT语句
格式
insert into Table2 (field1 ,field2,...) select value1,value2,... from Table1
或者
insert into Table2 select * from Table1
要求目标表Table2必须存在
insert into product2(pname,price) select pname,price from product;insert into product3 select category_id,count(*) from product group by category_id;9正则表达式
正则表达式(regular expression)描述了一种字符串匹配的规则正则表达式本身就是一个字符串使用这个字符串来描述、用来定义匹配规则匹配一系列符合某个句法规则的字符串。在开发中正则表达式通常被用来检索、替换那些符合某个规则的文本
MySQL通过REGEXP关键字支持正则表达式进行字符串匹配
格式
模式描述^匹配输入字符串的开始位置$匹配输入字符串的结束位置.匹配除\n之外的任何单个字符[...]字符集合。匹配所包含的任意一个字符。例如[abc]可以匹配plain中的a[^...]负值字符集合。匹配未包含的任意字符。例如[^abc]可以匹配plain中的pp1|p2|p3匹配p1或p2或p3。例如z/ food能匹配z”或food。(zlf)ood则匹配zood或food
格式描述*匹配前面的子表达式零次或多次。例如zo*能匹配z以及zoo。*等价于{0,}匹配前面的子表达式一次或多次。例如zo能匹配zo以及zoo但不能匹配z。等价于{1,}{n}n是一个非负整数。匹配确定的n次。例如o{2}不能匹配Bob中的o但是能匹配food中的两个o{n,m}m和n均为非负整数其中n m。最少匹配n次且最多匹配m次?出现0次或者1次
--^在字符串开始进行匹配
select abc REGEXP ^a;--1
select * from product where pname REGEXP ^海;--$在字符串末尾开始匹配
select abc REGEXP a$;
select abc REGEXP a$;--.匹配除换行符以外的任意字符
select abc REGEXP .b;--1
select abc REGEXP .c;--1
select abc REGEXP a.;--1--[...]匹配括号内的任意单个字符
select abc REGEXP [xyz];--0
select abc REGEXP [xaz];--1--[^...]取反
select a REGEXP [^abc];--0
select x REGEXP [^abc];--1
select abc REGEXP [^a];--1