厦门专业网站设计代理,百度收录有什么好处,wordpress添加分类筛选手表,沈阳哪个医院人流好一点Mysql高级语句#xff08;进阶查询语句MySQL数据库函数连接查询#xff09; 一、mysql查询语句1.1、 select ----显示表格中一个或数个字段的所有数据记录1.2、 distinct ----不显示重复的数据记录1.3、where ----有条件查询1.4、 and or ----且 或1.5 、in----显示已知的值的… Mysql高级语句进阶查询语句MySQL数据库函数连接查询 一、mysql查询语句1.1、 select ----显示表格中一个或数个字段的所有数据记录1.2、 distinct ----不显示重复的数据记录1.3、where ----有条件查询1.4、 and or ----且 或1.5 、in----显示已知的值的数据记录1.6、between----显示两个值范围内的数据记录1.7、 通配符1.8 、like ----模糊匹配1.9 、order by1.10、 group by ----汇总分组1.11、 having1.12 、别名 ----字段別名 表格別名1.13 、子查询语句1.14、exists 二、MySQL数据库函数2.1、数学函数2.2、 聚合函数2.3、 字符串函数 三、连接查询3.1、 表连接3.2、 union语句3.3、 多表查询之求交集值3.4、 多表查询之求无交集值 四、SQL语句执行顺序 一、mysql查询语句
先建立lilade数据库在建立nba表和cba表用于测试
create database lilade;create table lilade.nba (id int,name char(4), age int, sex char(2), hobby varchar(20));create table lilade.cba (id int,name char(4), age int, sex char(2), hobby varchar(20)); 1.1、 select ----显示表格中一个或数个字段的所有数据记录
语法
select 字段 from 表名;
例子
select * from nba;select name from nba; 1.2、 distinct ----不显示重复的数据记录
语法
select distinct 字段 from 表名
#例子
select distinct name from nba; 1.3、where ----有条件查询
where是对源语句进行条件查询。
语法
select 字段 from 表名 where 条件;
#例子
select store_name from laozi where sales 1000; 1.4、 and or ----且 或
语法
select 字段 from 表名 where 条件1 {[and|or] 条件2} ;
#例子
select store_name from laozi where sales 1000 or (sales 500 and sales 200); 1.5 、in----显示已知的值的数据记录
语法
select 字段 from 表名 where 字段 in (值1, 值2, ...);
#例子
select * from laozi where store_name in (los angeles, houston); 1.6、between----显示两个值范围内的数据记录
语法select 字段 from 表名 where 字段 between 值1 and 值2;
#例子
select * from laozi where date between 2020-12-06 and 2020-12-10; 1.7、 通配符
#语法
select 字段名 from 表名 where 字段 like 模式通配符含义%表示零个一个或者多个字符_下划线表示单个字符A_Z所有以A开头 Z 结尾的字符串 ‘ABZ’ ‘ACZ’ ACCCCZ’不在范围内 下划线只表示一个字符 AZ 包含a空格zABC%所有以ABC开头的字符串 ABCD ABCABC%CBA所有以CBA结尾的字符串 WCBA CBACBA%AN%所有包含AN的字符串 los angeles_AN%所有 第二个字母为 A 第三个字母 为N 的字符串
1.8 、like ----模糊匹配
一般和通配符配合使用。模糊匹配默认会扫描全表索引不生效。
语法
select 字段 from 表名 where 字段 like {模式};
#例子
select * from laozi where store_name like %os%; 1.9 、order by
按关键字排序
语法
select 字段 from 表名 [where 条件] order by 字段 [asc, desc];
#asc 是按照升序进行排序的是默认的排序方式。
#desc 是按降序方式进行排序。
#例子
select store_name,sales,date from laozi order by sales desc; 1.10、 group by ----汇总分组 对group by后面的字段的查询结果进行汇总分组通常是结合聚合函数一起使用的。 group by 有一个原则凡是在 group by 后面出现的字段必须在 select 后面出现 凡是在 select 后面出现的、且未在聚合函数中出现的字段必须出现在 group by 后面。
语法
select 字段1, sum(字段2) from 表名 group by 字段1;
#例子
select store_name, sum(sales) from laozi group by store_name order by sales desc; select store_name,count(store_name) from laozi group by store_name; 1.11、 having 对group by语句的结果进行条件筛选。 用来过滤由 group by 语句返回的记录集通常与 group by 语句联合使用. having 语句的存在弥补了 where 关键字不能与聚合函数联合使用的不足。
语法
select 字段1, sum(字段2) from 表格名 group by 字段1 having (函数条件);
#举个例子
select store_name, sum(sales) from laozi group by store_name having sum(sales) 1500; 1.12 、别名 ----字段別名 表格別名
as可省略仅在当前SQL语句生效。
语法
select 表格別名.字段1 [as] 字段別名 from 表格名 [as] 表格別名;
#例子
select a.store_name store, sum(a.sales) as total sales from laozi as a group by a.store_name; 1.13 、子查询语句
连接表格在where 子句或 having 子句中插入另一个 sql 语句。
语法
select 字段1 from 表格1 where 字段2 [比较运算符] (select 字段1 from 表格2 where 条件);
#外查询 (#内查询)
#内查询的结果作为外查询的参数[比较运算符]
#可以是符号的运算符例如 、、、、
#也可以是文字的运算符例如 like、in、between
#例子
select sum(sales) from laozi where store_name in (select store_name from location where region West); #举个例子2
select sum(A.sales) from laozi as A where A.store_name in (select store_name from location as B where B.store_name A.store_name);
#store_info表 别名为A表在当前语句中可以直接用a代替store_info使用
#location表 别名为B表 1.14、exists 用来测试内查询有没有产生任何结果。 如果有系统就会执行外查询中的sql语句; 如果没有那整个 SQL语句就不会产生任何结果
语法
select 字段1 from 表格1 where exists (select * from 表格2 where 条件;
#例子
select sum(sales) from laozi where exists (select * from location where region West);select sum(sales) from laozi where exists (select store_name from location where region Westt); 二、MySQL数据库函数
2.1、数学函数
数学函数功能abs(x)返回 x 的绝对值rand()返回 0 到 1 的随机数mod(x,y)返回 x 除以 y 以后的余数power(x,y)返回 x 的 y 次方round(x)返回离 x 最近的整数round(x,y)保留 x 的 y 位小数四舍五入后的值sqrt(x)返回 x 的平方根truncate(x,y)返回数字 x 截断为 y 位小数的值ceil(x)返回大于或等于 x 的最小整数floor(x)返回小于或等于 x 的最大整数greatest(x1,x2…)返回集合中最大的值也可以返回多个字段的最大的值least(x1,x2…)返回集合中最小的值也可以返回多个字段的最小的值
select abs(-1), rand(), mod(5,3), power(2,3), round(1.89); select round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1); 2.2、 聚合函数
聚合函数功能avg()返回指定列的平均值count( 字段 )返回指定列中非 NULL 值的个数行数count(*)返回指定列中所有行数不忽略NULL值min( )返回指定列的最小值max( )返回指定列的最大值sum(x)返回指定列的所有值之
select avg(sales) from laozi; select count(store_name) from laozi; select count(distinct store_name) from laozi; select max(sales) from laozi; select min(sales) from laozi; select sum(sales) from laozi; 2.3、 字符串函数
字符串函数功能trim()返回去除指定格式的值concat(x,y)将提供的参数 x 和 y 拼接成一个字符串substr(x,y)获取从字符串 x 中的第 y 个位置开始的字符串跟substring()函数作用相同substr(x,y,z)获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串length(x)返回字符串 x 的长度replace(x,y,z)替换将字符串 z 替代字符串 x 中的字符串 yupper(x)将字符串 x 的所有字母变成大写字母lower(x)将字符串 x 的所有字母变成小写字母left(x,y)返回字符串 x 的前 y 个字符right(x,y)返回字符串 x 的后 y 个字符repeat(x,y)将字符串 x 重复 y 次space(x)返回 x 个空格strcmp(x,y)比较 x 和 y返回的值可以为-1,0,1reverse(x)将字符串 x 反转
1trim
#示例1从名字开头的开始移除Sun Dasheng中的Sun显示
select trim(leading ‘Sun’ from ‘Sun Dasheng’);
select trim([ [位置] [要移除的字符串] from ] 字符串);
#[位置]的值可以为 leading (起头), trailing (结尾), both (起头及结尾)。
#[要移除的字符串]从字串的起头、结尾或起头及结尾移除的字符串。缺省时为空格。
#子查询语句select 嵌套selectselect trim(leading Los from (select store_name from location where store_nameLos Angeles));select trim( trailing York from (select store_name from location where store_nameNew York)); 2concat
字段名 不要加
字符串 要加 select concat (region , ,store_name) from location; select region|| || store_name from location; 3substr
select substr(store_name,5) from location where store_name Los Angeles;select substr(store_name,5,6) from location where store_name Los Angeles; 4length select replace(region,stern,st),store_name,length(store_name) from location; 5replace
select replace (region,st,stern) from location; 三、连接查询
3.1、 表连接
表连接概述innerjoin内连接 只返回两个表中联结字段相等的行记录left join左连接返回包括左表中的所有记录和右表中联结字段相等的记录不相等的部分返回NULLright join右连接返回包括右表中的所有记录和左表中联结字段相等的记录不相等的部分返回NULLunion联集将两个select查询语句的结果合并并去重union all联集将两个select查询语句的结果合并不去重
3.2、 union语句 联集将两个sql语句的结果合并起来两个sql语句所产生的字段需要是同样的数据记录种类。 union 生成结果的数据记录值将没有重复且按照字段的顺序进行排序。
语法
[select 语句 1] union [select 语句 2];
select store_name from location union select store_name from laozi; union all 将生成结果的数据记录值都列出来无论有无重复语法
[select 语句 1] union all [select 语句 2];
select store_name from location union all select store_name from laozi; 3.3、 多表查询之求交集值
取两个SQL语句结果的交集。
基本语法
select A.字段 from 左表 A inner join 右表 B on A.字段 B.字段;
select A.字段 from 左表 A inner join 右表 B using(同名字段);select A.字段 from 左表 A, 右表 B where A.字段 B.字段;select A.字段 from 左表 A where A.字段 in (select B.字段 from 右表 B);select A.字段 from 左表 A left join 右表 B on A.字段 B.字段 where B.字段 is not null;
select B.字段 from 左表 A right join 右表 B on A.字段 B.字段 where A.字段 is not null;
#求交集
#方式一
select A.store_name from location A inner join laozi B on A.store_name B.store_name; #方式二
select A.store_name from location A inner join laozi B using (store_name); #取两个SQL语句结果的交集且没有重复
select distinct A.store_name from location A inner join laozi B on A.store_name B.store_name;select distinct A.store_name from location A inner join laozi B using (store_name);select distinct A.store_name from location A inner join laozi B using (store_name) where B.store_name is not NULL; select A.store_name from (select distinct store_name from location union all select distinct store_name from laozi) A group by A.store_name having count( *) 1;#首先子查询select distinct store_name from location从“location”表中选择所有不重复的店铺名称。
#然后子查询select distinct store_name from store_info从“store_info”表中选择所有不重复的店铺名称。
#使用union all将两个子查询的结果合并并作为临时表A。
#最后对临时表A按照店铺名称进行分组使用having count(*) 1筛选出出现次数大于1的店铺名称。 3.4、 多表查询之求无交集值
显示第一个SQL语句的结果且与第二个SQL语句没有交集的结果且没有重复。求左表无交集
select A.字段 from 左表 A left join 右表 B on A.字段 B.字段 where B.字段 is null;select 字段 from 左表 where 字段 not in (select 字段 from 右表);求右表无交集
select B.字段 from 左表 A right join 右表 B on A.字段 B.字段 where A.字段 is null;select 字段 from 右表 where 字段 not in (select 字段 from 左表);求多表的无交集
select A.字段 from (select distinct 字段 from 左表 union all select distinct 字段 from 右表) A group by A.字段 having count(A.字段)1;
select distinct store_name from location where (store_name) not in ( select store_name from laozi);#子查询select store_name from store_info从store_info表中选择所有的店铺名称。
#主查询select distinct store_name from location从location表中选择所有不重复的店铺名称。
#使用where (store_name) not in条件将主查询中的店铺名称过滤掉那些在子查询结果中出现的店铺名称。 select distinct A.store_name from location A left join laozi B using (store_name) where B.store_name is NULL;#使用left join将location表作为左表记为A和store_info表作为右表记为B按照店铺名称进行连接。
#使用using (store_name)条件指定以店铺名称为连接的字段。
#使用where B.store_name is NULL条件过滤掉在连接结果中店铺名称在location表中出现但在store_info表中没有匹配的记录。
#最后使用distinct关键字来返回不重复的店铺名称。 select A.store_name from (select distinct store_name from location union all select distinct store_name from laozi) as A group by A.store_name having count(*)1;#子查询select distinct store_name from location从location表中选择所有不重复的店铺名称。
#子查询select distinct store_name from store_info从store_info表中选择所有不重复的店铺名称。
#使用union all将两个子查询的结果合并。
#将合并结果作为临时表A并使用as A来给临时表起一个别名。
#在临时表A的基础上使用group by A.store_name对店铺名称进行分组。
#使用having count(*) 1筛选出出现次数为1的店铺名称。 四、SQL语句执行顺序
FROM
left tableON
join_condition
join_typeJOIN
right_tableWHERE
where conditionGROUP BY
group_by_listHAVING
having_conditionSELECTDISTINCT
select listORDER BY
order_by_conditionLIMIT
limit number########################################################################################################
在SQL中一般而言SQL查询语句的执行顺序如下1. FROM指定要查询的数据表或视图。
2. JOIN根据指定的条件连接多个表。
3. WHERE基于指定的条件筛选出符合要求的行。
4. GROUP BY按照指定的列进行分组。
5. HAVING对分组后的结果进行条件筛选。
6. SELECT选择要返回的列。
7. DISTINCT去除重复的行。
8. ORDER BY按照指定的列进行排序。
9. LIMIT/OFFSET限制返回的结果数量和起始位置。
小结 go
order by 字段 ASC|DESC #排序
group by 字段 #分组
group by 字段 having 条件表达式 #根据group by分组后的结果再进行条件过滤表连接
inner join 内连接只返回两个表的字段相等的行记录
left join 左连接返回左表所有的行记录和右表字段相等的行记录不相等的行返回NULL
right join 右连接返回右表所有的行记录和左表字段相等的行记录不相等的行返回NULL
union 联集将两个select查询语句的结果合并并去重
union all 联集将两个select查询语句的结果合并不去重求交集
select A.字段 from 左表 A inner join 右表 B on A.字段 B.字段;
select A.字段 from 左表 A inner join 右表 B using(同名字段);select A.字段 from 左表 A, 右表 B where A.字段 B.字段;select A.字段 from 左表 A where A.字段 in (select B.字段 from 右表 B);select A.字段 from 左表 A left join 右表 B on A.字段 B.字段 where B.字段 is not null;
select B.字段 from 左表 A right join 右表 B on A.字段 B.字段 where A.字段 is not null;求左表无交集
select A.字段 from 左表 A left join 右表 B on A.字段 B.字段 where B.字段 is null;select 字段 from 左表 where 字段 not in (select 字段 from 右表);求右表无交集
select B.字段 from 左表 A right join 右表 B on A.字段 B.字段 where A.字段 is null;select 字段 from 右表 where 字段 not in (select 字段 from 左表);求多表的无交集
select A.字段 from (select distinct 字段 from 左表 union all select distinct 字段 from 右表) A group by A.字段 having count(A.字段)1;