MySQLクエリ文使用総括(一)——単一テーブルクエリ
16811 ワード
MySQLクエリ文使用総括(一)——単一テーブルクエリ
テーブルからのクエリーに必要なデータはselectコマンドを使用します.selectコマンドの構文は次のとおりです.
一、データ準備:emp(従業員)表を作成し、コードは以下の通りである.
emp(従業員)テーブルに必要なデータを挿入します.コマンドは次のとおりです.
二、表のすべての内容を表示します.これは最も基本的なselectコマンドで、コマンドは以下の通りです.
三、表の一部のフィールドを表示し、フィールドの別名を指定します.コマンドは次のとおりです.
四、where条件付きクエリー:
関係演算子:>,=,<=,!(<>)論理演算子:and,orその他の演算子:between...and...,in,not in,is null,is not null
五、ワイルドカードを使用したクエリー
MySQLでは、ワイルドカードを使用してあいまいなクエリーを実行します.ワイルドカードには、1、パーセント(%):任意の複数の文字2、アンダースコア(%)が一致することを示す2つの文字があります.ワイルドカードを使用している場合は、LIKE演算子を使用する必要があります.コマンドは次のとおりです.
説明:ワイルドカードを使用し、インデックスを使用する場合は、文字列の先頭にワイルドカードを置くことはできません.すなわち、e_name like‘張%’:インデックスe_を使用できますname like'%張':インデックスは使用できません
六、order by文を使用してクエリー結果をソートし、コマンドは以下の通りです.
七、集約関数を使用してクエリー結果をまとめる
集約関数を使用してクエリー結果を統計および要約できます.一般的な集約関数は、(1)count(*)関数:クエリー結果を統計する行数、パラメータは一般的に(*)です.(2)sum(数値型フィールド)関数:1つの数値型フィールドを合計するために使用されます.(3)avg(数値型フィールド)関数:1つの数値型フィールドを平均するために使用される.(4)max(フィールド)関数:フィールドの最大値を返します.(5)min(フィールド)関数:フィールドの最小値を返します.コードは次のとおりです.
説明:集約関数を使用する場合、selectは一般的にフィールド名を配置しません.
八、groupbyによるグループクエリー
1、集約関数はクエリー結果全体を統計してまとめ、groupbyを使用してクエリー結果をグループ化し、集約関数を使用して各グループのデータをそれぞれ統計してまとめます.2、whereとhavingの違い:パケットクエリーでwhereが使用されている場合は、クエリー結果をwhereでフィルタしてからグループ化します.havingを使用している場合は、【グループ】をフィルタします.3、groupbyの後ろのフィールドは一般的にselectの後ろに置いて、パケットフィールドをクエリー結果に表示します.
九、limitページング機能の使用
フォーマット:limit[m,]n説明:最初のパラメータmは省略でき、開始インデックスを表し、mを指定しない場合、mはデフォルトで0であり、最初のレコードから始まることを表す.2番目のパラメータnは省略できず、m+1番目のレコードからn本のレコードを取ることを示す.コードは次のとおりです.
テーブルからのクエリーに必要なデータはselectコマンドを使用します.selectコマンドの構文は次のとおりです.
select [distinct] [as ]
from [join on ]
[where ]
[group by ]
[having ]
[order by ]
[limit ];
一、データ準備:emp(従業員)表を作成し、コードは以下の通りである.
create table emp(
e_id int primary key auto_increment comment ' ',
e_name char(20) comment ' ',
birth datetime comment ' ',
phone char(20) comment ' ',
salary decimal(10,2) comment ' ',
department char(20) comment ' '
);
emp(従業員)テーブルに必要なデータを挿入します.コマンドは次のとおりです.
insert into emp(e_name,birth,phone,salary,department)
values(' ','1990-1-23','13607589999',5000,' '),(' ','1994-10-3','13803732222',4500,' '),
(' ','1989-10-15','13903735874',5800,' '),(' ','1992-3-30','13703736666',6600,' '),
(' ','1989-3-25','13603736446',8000,' '),(' ','1997-12-5','13803731234',6700,' '),
(' ','1979-1-30','13503735888',9500,' '),(' ','1984-3-12','13603734848',9200,' '),
(' ','1989-6-6','13703736699',5900,' '),(' ','1992-11-3','13503736622',6800,' ');
二、表のすべての内容を表示します.これは最も基本的なselectコマンドで、コマンドは以下の通りです.
mysql> select * from emp;
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 1 | | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | |
| 2 | | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | |
| 3 | | 1989-10-15 00:00:00 | 13903735874 | 5800.00 | |
| 4 | | 1992-03-30 00:00:00 | 13703736666 | 6600.00 | |
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
| 6 | | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | |
| 7 | | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | |
| 8 | | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | |
| 9 | | 1989-06-06 00:00:00 | 13703736699 | 5900.00 | |
| 10 | | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | |
+------+-----------+---------------------+-------------+---------+------------+
10 rows in set (0.00 sec)
三、表の一部のフィールドを表示し、フィールドの別名を指定します.コマンドは次のとおりです.
mysql> select e_name as ,birth as ,salary as from emp;
+--------------+---------------------+--------------+
| | | |
+--------------+---------------------+--------------+
| | 1990-01-23 00:00:00 | 5000.00 |
| | 1994-10-03 00:00:00 | 4500.00 |
| | 1989-10-15 00:00:00 | 5800.00 |
| | 1992-03-30 00:00:00 | 6600.00 |
| | 1989-03-25 00:00:00 | 8000.00 |
| | 1997-12-05 00:00:00 | 6700.00 |
| | 1979-01-30 00:00:00 | 9500.00 |
| | 1984-03-12 00:00:00 | 9200.00 |
| | 1989-06-06 00:00:00 | 5900.00 |
| | 1992-11-03 00:00:00 | 6800.00 |
+--------------+---------------------+--------------+
10 rows in set (0.00 sec)
四、where条件付きクエリー:
関係演算子:>,=,<=,!(<>)論理演算子:and,orその他の演算子:between...and...,in,not in,is null,is not null
mysql> select * from emp
where salary>=7000 and department=' '; -- 7000
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
| 7 | | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | |
| 8 | | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp
where salary>7000 or salary<5000; -- 7000 5000
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 2 | | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | |
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
| 7 | | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | |
| 8 | | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | |
+------+-----------+---------------------+-------------+---------+------------+
4 rows in set (0.00 sec)
mysql> select * from emp
where salary between 6000 and 7000; -- 6000 7000
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 4 | | 1992-03-30 00:00:00 | 13703736666 | 6600.00 | |
| 6 | | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | |
| 10 | | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp
where salary not between 5000 and 7000; -- 5000 7000
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 2 | | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | |
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
| 7 | | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | |
| 8 | | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | |
+------+-----------+---------------------+-------------+---------+------------+
4 rows in set (0.00 sec)
mysql> select * from emp
where department in (' ',' '); --
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 1 | | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | |
| 2 | | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | |
| 3 | | 1989-10-15 00:00:00 | 13903735874 | 5800.00 | |
| 9 | | 1989-06-06 00:00:00 | 13703736699 | 5900.00 | |
| 10 | | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | |
+------+-----------+---------------------+-------------+---------+------------+
5 rows in set (0.00 sec)
mysql> select * from emp
where department not in (' ',' '); --
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 4 | | 1992-03-30 00:00:00 | 13703736666 | 6600.00 | |
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
| 6 | | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | |
| 7 | | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | |
| 8 | | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | |
+------+-----------+---------------------+-------------+---------+------------+
5 rows in set (0.00 sec)
五、ワイルドカードを使用したクエリー
MySQLでは、ワイルドカードを使用してあいまいなクエリーを実行します.ワイルドカードには、1、パーセント(%):任意の複数の文字2、アンダースコア(%)が一致することを示す2つの文字があります.ワイルドカードを使用している場合は、LIKE演算子を使用する必要があります.コマンドは次のとおりです.
mysql> select * from emp
where e_name like ' %'; --
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 1 | | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | |
| 2 | | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | |
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp
where e_name like ' _'; --
+------+--------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+--------+---------------------+-------------+---------+------------+
| 1 | | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | |
+------+--------+---------------------+-------------+---------+------------+
1 row in set (0.00 sec)
mysql> select * from emp
where e_name like '% '; -- 【 】
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
+------+-----------+---------------------+-------------+---------+------------+
1 row in set (0.01 sec)
mysql> select * from emp where e_name like '% %'; -- 【 】
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 6 | | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | |
| 7 | | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | |
+------+-----------+---------------------+-------------+---------+------------+
2 rows in set (0.00 sec)
説明:ワイルドカードを使用し、インデックスを使用する場合は、文字列の先頭にワイルドカードを置くことはできません.すなわち、e_name like‘張%’:インデックスe_を使用できますname like'%張':インデックスは使用できません
六、order by文を使用してクエリー結果をソートし、コマンドは以下の通りです.
mysql> select *
from emp where e_name like ' %' order by department desc;
-- department
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 6 | | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | |
| 9 | | 1989-06-06 00:00:00 | 13703736699 | 5900.00 | |
| 10 | | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | |
| 3 | | 1989-10-15 00:00:00 | 13903735874 | 5800.00 | |
+------+-----------+---------------------+-------------+---------+------------+
4 rows in set (0.00 sec)
mysql> select e_name,department,birth,salary
from emp order by department,birth desc;
-- department , birth
+-----------+------------+---------------------+---------+
| e_name | department | birth | salary |
+-----------+------------+---------------------+---------+
| | | 1994-10-03 00:00:00 | 4500.00 |
| | | 1990-01-23 00:00:00 | 5000.00 |
| | | 1989-10-15 00:00:00 | 5800.00 |
| | | 1992-11-03 00:00:00 | 6800.00 |
| | | 1989-06-06 00:00:00 | 5900.00 |
| | | 1997-12-05 00:00:00 | 6700.00 |
| | | 1992-03-30 00:00:00 | 6600.00 |
| | | 1989-03-25 00:00:00 | 8000.00 |
| | | 1984-03-12 00:00:00 | 9200.00 |
| | | 1979-01-30 00:00:00 | 9500.00 |
+-----------+------------+---------------------+---------+
10 rows in set (0.00 sec)
七、集約関数を使用してクエリー結果をまとめる
集約関数を使用してクエリー結果を統計および要約できます.一般的な集約関数は、(1)count(*)関数:クエリー結果を統計する行数、パラメータは一般的に(*)です.(2)sum(数値型フィールド)関数:1つの数値型フィールドを合計するために使用されます.(3)avg(数値型フィールド)関数:1つの数値型フィールドを平均するために使用される.(4)max(フィールド)関数:フィールドの最大値を返します.(5)min(フィールド)関数:フィールドの最小値を返します.コードは次のとおりです.
mysql> select ' ' as ,count(*) as from emp
where department=' '; --
+-----------+--------------+
| | |
+-----------+--------------+
| | 5 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql> select ' ' as ,sum(salary) as from emp
where department=' ';--
+-----------+--------------+
| | |
+-----------+--------------+
| | 40000.00 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql> select ' ' as ,avg(salary) as from emp
where department=' ';--
+-----------+--------------+
| | |
+-----------+--------------+
| | 8000.000000 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql> select max(salary) as from emp; --
+--------------+
| |
+--------------+
| 9500.00 |
+--------------+
1 row in set (0.00 sec)
mysql> select min(salary) as from emp; --
+--------------+
| |
+--------------+
| 4500.00 |
+--------------+
1 row in set (0.00 sec)
説明:集約関数を使用する場合、selectは一般的にフィールド名を配置しません.
八、groupbyによるグループクエリー
1、集約関数はクエリー結果全体を統計してまとめ、groupbyを使用してクエリー結果をグループ化し、集約関数を使用して各グループのデータをそれぞれ統計してまとめます.2、whereとhavingの違い:パケットクエリーでwhereが使用されている場合は、クエリー結果をwhereでフィルタしてからグループ化します.havingを使用している場合は、【グループ】をフィルタします.3、groupbyの後ろのフィールドは一般的にselectの後ろに置いて、パケットフィールドをクエリー結果に表示します.
mysql> select department as ,count(*) as ,avg(salary) as
from emp group by deepartment; --
+-----------+--------------+--------------+
| | | |
+-----------+--------------+--------------+
| | 3 | 5100.000000 |
| | 2 | 6350.000000 |
| | 5 | 8000.000000 |
+-----------+--------------+--------------+
3 rows in set (0.00 sec)
mysql> select department as ,count(*) as from emp
where salary>=5500 group by department; -- 5500
+-----------+--------------+
| | |
+-----------+--------------+
| | 1 |
| | 2 |
| | 5 |
+-----------+--------------+
3 rows in set (0.01 sec)
mysql> select department as ,count(*) as ,avg(salary) as
from emp group by department having avg(salary)>=6000;
-- , 6000
+-----------+--------------+--------------+
| | | |
+-----------+--------------+--------------+
| | 2 | 6350.000000 |
| | 5 | 8000.000000 |
+-----------+--------------+--------------+
2 rows in set (0.00 sec)
九、limitページング機能の使用
フォーマット:limit[m,]n説明:最初のパラメータmは省略でき、開始インデックスを表し、mを指定しない場合、mはデフォルトで0であり、最初のレコードから始まることを表す.2番目のパラメータnは省略できず、m+1番目のレコードからn本のレコードを取ることを示す.コードは次のとおりです.
mysql> select * from emp order by salary desc limit 3; -- 3
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 7 | | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | |
| 8 | | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | |
| 5 | | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp order by birth desc limit 1;
-- (birth )
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 6 | | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | |
+------+-----------+---------------------+-------------+---------+------------+
1 row in set (0.00 sec)