データベース管理と高可用性第5章SQLの高度なステートメント
1.1:キーワードで並べ替え
SELECT column1,column2,...FROM table_name ORDER BY column1,column2,...
ASC|DESC;
mysql> select id,name,score from accp where score>80 order by score desc;
1.2:複数のフィールドで並べ替え
mysql> select id,name,score from accp where score>80 order by score desc,id desc;
//注意每个字段都要写asc(默认)或者desc
1.3:結果をグループ化する
SELECT column_name,aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
1.3.1:同じタイプをグループにグループ化することによるグループ化
mysq> select count(name),level from player where level>=45 group by level;
mysql> select count(name),score,age from kgc where score>=80 group by score;
+-------------+-------+-----+
| count(name) | score | age |
+-------------+-------+-----+
| 1 | 80.00 | 20 |
| 2 | 88.00 | 18 |
| 1 | 90.00 | 28 |
+-------------+-------+-----+
1.3.2:GROUPBY結合ORDERBY
mysql> select count(name),score,age from kgc where score>=80 group by score order by count(name) desc;
+-------------+-------+-----+
| count(name) | score | age |
+-------------+-------+-----+
| 2 | 88.00 | 18 |
| 1 | 90.00 | 28 |
| 1 | 80.00 | 20 |
+-------------+-------+-----+
1.4:結果エントリを制限する
SELECT column1,column2,....FROM table_name LIMIT[offset,] number;
number 返回记录行的最大数目
[offset,] 位置偏移量,从0开始
[a,b] 索引起始值 b代表包括a-1的行开始往下b行
mysql> select id,name from kgc limit 3; //显示前三条数据 起始位是0代表第一条数据,代表第一行
mysql> select id,name from kgc limit 2,3; 起始位2,代表第三条记录,第 3 条记录开始显示之后的 3条数据。
1.5:エイリアスを設定する
SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s)FROM table_name AS alias_name;
*字段的别名
mysql> select count(*) as number from player;
+--------+
| number |
+--------+
|3218 |
+--------+
*表的别名
mysql> select a.name,h.hob_name from accp a inner join Hob h on a.hobby=h.id;
把accp表 as a, Hob表 as h 定义别名,as可以省略
from 表 表的别名
1.6:ワイルドカードをlikeと組み合わせて使用する
mysql> select id,name,score from accp where name like 'ss%';
// like ss开头%任意结尾
mysql> select id,name,score from accp where name like '%s%';
//like s 两边任意字符
mysql> create table bbb as select * from kgc; 复制表
1.7:サブクエリ
*IN语句是用来判断某个值是否在给定的结果集中,判断是否等于集合中的某个值
mysql> select name,hobby from accp where hobby in (select id from Hob where hob_name='云计算');
mysql> select name,hobby from accp where hobby in (select id from Hob);
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| lisi | 1 |
| wangwu | 3 |
| zhangsan | 2 |
+----------+-------+
//先执行()里的select语句,把结果的集合作为前面select语句的查询条件
mysql> delete from tmp where id in (select a.id from (select id from tmp where age=20) a);
//把(select id from tmp where age=20)的结果定义别名给a
1.8:NULL値
*空值长度为0,不占空间;NULL值的长度为NULL,占用NULL这个字符空间 NULL就好比"真空" 空值好比"空气"
//Conut计数空值会计入统计
//COUNT计数时NULL会被忽略
//空值不是NULL
mysql> select * from num where name is not null;
+----+------+
| id | name |
+----+------+
| 1 | tom |
| 3 | |
+----+------+
mysql> select * from num where name is null;
+----+------+
| id | name |
+----+------+
| 2 | NULL |
+----+------+
1.9:正規表現
1.9.1:特定の文字列で始まるレコード
mysql> select id,name,score from accp where name regexp '^li';
1.9.2:特定の文字列で終わるレコード
mysql> select id,name,score from accp where name regexp 'wu$';
1.9.3:指定された文字列を含むレコード
mysql> select id,name,score from accp where name regexp 'an';
1.9.4:「。」を使用して、文字列内の文字レコードを置き換えます
mysql> select id,name,score from accp where name regexp 'zhang.an'; //regexp英文就是正则表达式的意思
1.9.5:または関係するレコードを照合する
mysql> select id,name,score from accp where name regexp 'an|si';
1.9.6:前の文字をいくつでも一致させる
mysql> insert into num (id,name) values (4,'oooo'),(5,'ooooo');
1.9.7:「+」は前の文字と少なくとも1回一致します
mysql> select id,name from num where name regexp 'ooooo+';
1.9.8:指定された文字セットのいずれかに一致します
mysql> select id,name from num where name regexp '^[a-z]';
1.10:オペレーター
1.10.1:算術演算子
mysql> select 1+2,2*3,5-4,7%2,7/2;
1.10.2:比較演算子
---等于运算符
mysql> select 2=4,2='2','e'='e',(2+2)=(3+1),'r'=NULL;
0 | 1 | 1 | 1 | NULL
---不等于运算符
mysql> select 'kgc'!='bdqn',12<>13,NULL<>NULL;
| 1 | 1 | NULL
---大于、大于等于、小于、小于等于运算符
条件成立返回1,不成立返回0
---IS NULL、IS NOT NULL
IS NULL 判断一个值是否为 NULL,如果为 NULL 返回 1,否则返回 0。
IS NOT NULL 判断一个值是否不为 NULL,如果不为 NULL 返回 1,否则返回 0
---BETWEEN AND
BETWEEN AND 比较运算通常用于判断一个值是否落在某两个值之间.
mysql> select 4 between 2 and 6,5 between 5 and 10,'f' between 'a' and 'z';
| 1 | 1 | 1 |
---LEAST、GREATEST (最小值,最大值)
---IN NOT IN IN在对应的列表里返回1,否则返回0 NOT IN 返回值相反
1.11:論理演算子
AND && AND all 1 is 1
or || OR sees 1 then 1
not not!Inverted
Exclusive OR XOR all zeros is zero、all 1s(non-zero)is zero、one zero and one non-zero is 1
1.12:ビット演算子-バイナリ数の計算
オペレーター | 説明 |
---|---|
& | ビットワイズと |
〜 | ビット単位の否定 |
^ | ビットワイズXOR |
<< | 左へのビットシフト |
>> | ビットシフト右 |
AND演算とOR演算について
// AND演算、すべての1が1である
1010年10
1111年15
結果
1010 = 10
//あるいは操作することは1見ている
1010
1111
結果
1111 = 15
//排他的論理和演算
1010
1111
結果
= 5 0101
左シフト演算子は左にシフトし、余分な部分は削除され、空の部分はゼロで埋められます。
右シフト演算子は右にシフトし、余分な部分は削除されます。
「15 >> 2」は、数値15を1111のバイナリに変換し、2ビットを右にシフトします。右側の11の2ビットは破棄されて11になり、左側は00で埋められ、最終的にバイナリで0011になります。これは、10進数に変換すると3になります。
1111右に2ビットシフト
前にゼロを11塗りつぶし
0011
に2つのビットを有する0010シフトが左
後ろにゼロで10塗りを
1000の
// 5&〜1手段5の反転動作と1
0001反転1110
1110和演算は、次いで、1全て1である
0101
の結果
0100 = 4
1.13オペレーターの優先順位
レベルの高い演算子が最初に計算されます。演算子が同じレベルの場合、MySQLは左から右の順に計算します。
優先度 | オペレーター | 優先度 | オペレーター |
---|---|---|---|
1 | ! | 8 | |
2 | 〜 | 9 | =、<=>、> =、>、<=、<、<>、!=、IS、LIKE、REGEXP、IN |
3 | ^ | 10 | BETWEEN、CASE、WHEN、THEN、ELSE |
4 | *、/(DIV)、%(MOD) | 11 | ない |
5 | +、- | 12 | &&、そして |
6 | >>、<< | 13 | |
7 | & | 14 | := |
「!」の優先度が最も高く、「:=」の優先度が最も低くなります。
1.14接続クエリ
1.14.1内部結合交差
select a.列,b.列 from a表 a(别名) inner join b表 b(别名) on a(列)=b(列);
mysql> select a.name,h.hob_name from accp a inner join Hob h on a.hobby=h.id;
+----------+--------------+
| name | hob_name |
+----------+--------------+
| wangwu | 大数据 |
| lisi | 云计算 |
| wangwu | 人工智能 |
| zhangsan | 大数据 |
+----------+--------------+
1.4.2左接続
左表是主表,全显示,右表是辅表,匹配到才显示,匹配不到的NULL
select a.列,b.列 from a表 a(别名) left join b表 b(别名) on a(列)=b(列);
mysql> select a.name,h.hob_name from accp a left join Hob h on a.hobby=h.id;
+----------+--------------+
| name | hob_name |
+----------+--------------+
| lisi | 云计算 |
| wangwu | 大数据 |
| zhangsan | 大数据 |
| wangwu | 人工智能 |
| zhaoliu | NULL |
| tianqi | NULL |
+----------+--------------+
1.4.2正しい接続
右表主表列全显示,左表匹配到的显示,匹配不到的null
select a.列,b.列 from a表 a(别名) right join b表 b(别名) on a(列)=b(列);
mysql> select a.name,h.hob_name from accp a right join Hob h on a.hobby=h.id;
+----------+--------------+
| name | hob_name |
+----------+--------------+
| wangwu | 大数据 |
| lisi | 云计算 |
| wangwu | 人工智能 |
| zhangsan | 大数据 |
| NULL | 大数据 |
+----------+--------------+