Mysql の高度なステートメント (高度なクエリ ステートメント、データベース関数、接続クエリ)

1.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 ---- テーブル内の 1 つまたは複数のフィールドのすべてのデータ レコードを表示します

语法
select "字段" from "表名";
例子
select * from nba;

ここに画像の説明を挿入します

select name from nba;

ここに画像の説明を挿入します

1.2. 個別 ---- 重複したデータレコードを表示しません

语法
select distinct "字段" from "表名"
#例子
select distinct name from nba;

ここに画像の説明を挿入します

1.3. where ---- 条件付きクエリ

ここで、 はソース ステートメントの条件付きクエリです。

语法
select "字段" from "表名" where "条件";
#例子
select store_name from laozi where sales > 1000;

ここに画像の説明を挿入します

1.4. およびまたは ----および または

语法
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----2 つの値の範囲内のデータ レコードを表示します

语法: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 模式
ワイルドカード 意味
% 0 個、1 個以上の文字を表します
_ アンダースコアは単一の文字を表します
A_Z A で始まり Z で終わるすべての文字列 ('ABZ' 'ACZ' 'ACCCCZ') は範囲内にありません。アンダースコアは 1 文字 AZ のみを表し、スペースと z が含まれます。
ABC% ABC で始まるすべての文字列 ABCD ABCABC
%CBA CBA WCBA CBACBA で終わるすべての文字列
%AN% AN ロサンゼルスを含むすべての文字列
_AN% 2 番目の文字が A、3 番目の文字が N であるすべての文字列

1.8、同様 ---- ファジーマッチング

  • 通常、ワイルドカードと組み合わせて使用​​されます。
  • あいまい一致はデフォルトでテーブル全体をスキャンし、インデックスは有効になりません。
语法
select "字段" from "表名" where "字段" like {
    
    模式};
#例子
select * from laozi where store_name like '%os%';

ここに画像の説明を挿入します

1.9、注文まで

キーワードで並べ替える

语法
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 の後に表示されるフィールドはすべて、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、有る

  • group by ステートメントの結果に対して条件付きフィルターを実行します。

  • group by ステートメントによって返されたレコード セットをフィルター処理するために使用され、通常は group by ステートメントと組み合わせて使用​​されます。

  • have ステートメントの存在により、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 句または Hasting 句に別の 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、存在します

  • 内部クエリが結果を生成するかどうかをテストするために使用されます。

  • その場合、システムは外部クエリ内の 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');

ここに画像の説明を挿入します

2. MySQLデータベース機能

2.1. 数学的関数

数学関数 関数
腹筋(x) xの絶対値を返します。
ランド() 0から1までの乱数を返します
mod(x,y) xをyで割った余りを返します。
べき乗(x,y) x の y 乗を返します
ラウンド(x) x に最も近い整数を返します
ラウンド(x,y) x の値を小数点以下 y 桁に四捨五入したままにする
sqrt(x) x の平方根を返します
切り捨て(x,y) 数値 x を小数点以下 y 桁に切り捨てた値を返します。
天井(x) x 以上の最小の整数を返します。
床(x) x 以下の最大の整数を返します。
最大(x1、x2…) コレクション内の最大値、または複数のフィールドの最大値を返します。
最小(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. 集計機能

集計関数 関数
平均() 指定された列の平均を返します
カウント(フィールド) 指定された列内のNULL以外の値の数(行数)を返します。
カウント(*) 指定された列のすべての行の数を返します。NULL 値は無視されません
分( ) 指定された列の最小値を返します。
最大( ) 指定された列の最大値を返します。
合計(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. 文字列関数

文字列関数 関数
トリム() 指定された形式を使用せずに値を返します
concat(x,y) 指定されたパラメータ x と y を文字列に連結します。
部分文字列(x,y) 文字列 x の y 番目の位置から始まる文字列を取得します。これは substring() 関数と同じ効果があります。
substr(x,y,z) 文字列 x の y 番目の位置から始まる長さ z の文字列を取得します。
長さ(x) 文字列 x の長さを返します
replace(x,y,z) 置換、文字列 x 内の文字列 z を文字列 y に置き換えます
上(x) 文字列 x のすべての文字を大文字に変換します
下(x) 文字列 x のすべての文字を小文字に変換します
左(x,y) 文字列 x の最初の y 文字を返します。
右(x,y) 文字列 x の最後の y 文字を返します。
繰り返し(x,y) 文字列を x y 回繰り返します
スペース(x) x 個のスペースを返します
strcmp(x,y) x と y を比較すると、戻り値は -1、0、1 になります。
逆(x) 文字列 x を反転します

1)トリム

#示例1:从名字开头的开始,移除Sun Dasheng中的Sun显示
select trim(leading ‘Sun’ from ‘Sun Dasheng’);
select trim([ [位置] [要移除的字符串] from ] 字符串);
#[位置]:的值可以为 leading (起头), trailing (结尾), both (起头及结尾)。 
#[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。

#子查询语句,select 嵌套select

select trim(leading 'Los' from (select store_name from location where store_name='Los Angeles'));

select trim( trailing 'York' from (select store_name from location where store_name='New York'));

ここに画像の説明を挿入します
2)連結

字段名 不要加 ' '
字符串 要加' '
select concat (region ,' ',store_name) from location;

ここに画像の説明を挿入します

select region|| ' ' || store_name from location;

ここに画像の説明を挿入します
3)部分文字列

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';

ここに画像の説明を挿入します
4)長さ

 select replace(region,'stern','st'),store_name,length(store_name) from location;

ここに画像の説明を挿入します
5)交換する

select replace (region,'st','stern') from location;

ここに画像の説明を挿入します

3. 接続クエリ

3.1. テーブル接続

表面 接続する 概要
内側 参加する 内部結合は、2 つのテーブル内の結合フィールドが等しい行のみを返します。
左結合 左結合 右側のテーブルの結合フィールドと等しい左側のテーブルのすべてのレコードを返し、等しくない部分については NULL を返します。
右結合 右結合 左側のテーブルの結合フィールドと等しい右側のテーブルのすべてのレコードを返し、等しくない部分については NULL を返します。
連合 連合 2 つの選択クエリ ステートメントの結果を結合し、重複を削除します。
すべてを結合する 連合 重複排除を行わずに 2 つの選択クエリ ステートメントの結果をマージします。

3.2、共用体ステートメント

  • Union は 2 つの SQL ステートメントの結果を結合します。2 つの 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. 交差値を見つけるための複数テーブルクエリ

2 つの 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的店铺名称。

ここに画像の説明を挿入します

4. SQL文の実行順序

FROM
<left table>

ON
<join_condition>
<join_type>

JOIN
<right_table>

WHERE
<where condition>

GROUP BY
<group_by_list>

HAVING
<having_condition>

SELECT

DISTINCT
<select list>

ORDER BY
<order_by_condition>

LIMIT
<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;


おすすめ

転載: blog.csdn.net/fyb012811/article/details/133307572