MySQLの基礎部分のまとめ
10539 ワード
MySQL
1、データベースの選択 use dbname show databases;
2、データテーブル show tables mysql> show columns from customers; mysql> desc customers;
3、show文 show status show create databases show create table show grants
4、select検索
4.1.1バージョン以降は大文字と小文字を区別しませんが、読みやすく、デバッグしやすいように使用することをお勧めします.
4-1、並べ替えデータ
4-2、並べ替え方向の指定 desc降順 asc昇順-デフォルト 注意順序from>ordrr by>limit
5、where条件
相関オペレータ:= <>は に等しくない != に等しくない< 未満> より大きい>= 以上<= 以下 between両者の間and andの優先度がorより大きく、orを処理する必要がある場合はカッコが必要です
ポイント:Null値チェック
NULL値:NULL
MySQLでNULLかどうかを判断する句は、IS NULL
example:
6、whereデータフィルタ
(logical operator)論理オペレータ:and-or
演算子の優先度の問題:次のSQLでは実際に
6-1、inオペレータ(not in)
6-2、orオペレータ
7、ワイルドカードでフィルタ
likeと_後者は1文字しか一致しないという違いがあります
7-1、like
**注意NULL%ワイルドカードは何でも一致するようですが、例外としてNULLがあります.WHERE prodでもname LIKE'%'も、値NULLを製品名とする行に一致しません.**
7-2、_
8、正規表現
likeはすべてにマッチし、REGEXPはすべてと部分にマッチします.
デフォルトでは大文字と小文字を区別せず、大文字と小文字を区別する必要がありますbinary
10、計算フィールド concatマージ2つのフィールドが新しいフィールドにマージされる rtrim(ltrim,trim)スペース を削除 as別名 計算 +、-、* 、\
11、関数 trim、ltrim、rtrim空の値 を削除 Upperを大文字 に変更
11-2時間関数 AddDate()日付(日、週など) を追加 AddTime()1時間(時、分等) を追加 CurDate()は、現在の日付 を返します. CurTime()は現在の時間 を返します.==Date()日付時刻を返す日付部分== DateDiff()計算2つの日付の差 Date_Add()高度に柔軟な日付演算関数 Date_Format()は、フォーマットされた日付または時間列 を返します. Day()は、1つの日付を返す日数部分 DayOfWeek()は、1つの日付に対して、対応する曜日 を返します. Hour()は、1時間分の時間部分 を返す Minute()は、1時間分の部分 を返す. Month()は、日付の月次部分 を返します. Now()は、現在の日付と時刻 を返します. Second()は、1時間の秒部分 を返す. Time()は、1つの日時の時間部分 を返す. Year()は、日付の年部分 を返します.
9月のある日のデータを取る
9月の全月間の注文を取る
11-4数値処理関数 Abs()は、1つの数の絶対値 を返します. Cos()は、角度のコサイン を返します. Exp()は、1つの数の指数値 を返します. Mod()は、除算動作の剰余 を返す. Pi()は、円周率 を返す. Rand()は、乱数 を返します. Sin()は、角度の正弦波 を返します. Sqrt()は、1つの数の平方根 を返す. Tan()は、角度の正接 を返します.
11-5集計関数 AVG()は、ある列の平均値 を返す. COUNT()は、ある列の行数 を返します. MAX()は、ある列の最大値 を返す. MIN()は、ある列の最小値 を返す. SUM()は、ある列の値の和 を返します. DISTINCT
データのグループ化
GROUP BY句とHAVING句
having
唯一の違いはWHEREフィルタラインであり、HAVINGフィルタパケットである.WHEREはデータパケット前にフィルタリングし、HAVINGはデータパケット後にフィルタリングする
じゅんじょ select from where group by having order by limit
12サブクエリ
15接続テーブル
デカルト積
2つのテーブルを同時にデータソース(from後のテーブル名)として使用し、マッチング条件を追加しない場合、結果セットはディカル積になります.ディカル積の結果は意味がありませんが、ディカル積は連合クエリー、接続クエリーの基礎です.
1.クロスコネクションcross join
表Aの1つのレコードを使用して、表Bにすべてのレコードを接続すると、デカルト積になります.
2.内部接続
selectフィールドリストfromテーブルA【inner】joinテーブルB、一致した成功したレコード
3.外部接続は左接続と右接続に分けられ、
左の接続は左のすべて、右の一致する部分を保持します.
4.usingキーワード
現在のノートは『MySQL必知必会』から
1、データベースの選択
2、データテーブル
3、show文
4、select検索
4.1.1バージョン以降は大文字と小文字を区別しませんが、読みやすく、デバッグしやすいように使用することをお勧めします.
mysql> select cust_name from customers;
mysql> select cust_name cust_status from customers;
mysql> select distinct vend_id from products;
mysql> select prod_name from products limit 5;
mysql> select prod_name from products limit 5,5;
//
0 ,limit5,5 5 ( ), 5 , :6:10
, MySQL5 :limit 4 offset 3, 3 4 , limit 3,4
4-1、並べ替えデータ
//
mysql> select prod_name from products order by prod_name;
// ,
mysql> select prod_id,prod_price,prod_name from products order by prod_price ,prod_name;
4-2、並べ替え方向の指定
mysql> select prod_id,prod_price,prod_name from products order by prod_price desc;
mysql> select prod_id,prod_price,prod_name from products order by prod_price asc;
mysql> select prod_price from products order by prod_price desc limit 1;
5、where条件
相関オペレータ:
mysql> select prod_price,prod_name from products where prod_price = 2.50;
mysql> select prod_price,prod_name from products where prod_price between 5 and 10;
// IS NULL
mysql> select cust_id from customers where cust_email is null;
ポイント:Null値チェック
NULL値:NULL
MySQLでNULLかどうかを判断する句は、IS NULL
example:
mysql> select cust_id FROM customers where cust_email IS NULL;
+---------+
| cust_id |
+---------+
| 10002 |
| 10005 |
+---------+
6、whereデータフィルタ
(logical operator)論理オペレータ:and-or
mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 and prod_price<= 10;
mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 or vend_id = 1002;
演算子の優先度の問題:次のSQLでは実際に
vend_id = 1002 and prod_price >= 10;
を実行し、vend_id = 1003
を実行します.andの優先度がorより大きいので、理想的に実行する場合はカッコを付けます!mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 or vend_id = 1002 and prod_price >= 10;
mysql> select prod_id,prod_price,prod_name from products where (vend_id = 1003 or vend_id = 1002 )and prod_price >= 10;
6-1、inオペレータ(not in)
mysql> select prod_id,prod_price,prod_name from products where vend_id in (1002,1003) order by prod_name;
6-2、orオペレータ
mysql> select prod_id,prod_price,prod_name from products where vend_id not in (1002,1003) order by prod_name;
7、ワイルドカードでフィルタ
likeと_後者は1文字しか一致しないという違いがあります
7-1、like
**注意NULL%ワイルドカードは何でも一致するようですが、例外としてNULLがあります.WHERE prodでもname LIKE'%'も、値NULLを製品名とする行に一致しません.**
mysql> select prod_id,prod_price,prod_name from products where prod_name LIKE 'jet%';
mysql> select prod_id,prod_price,prod_name from products where prod_name LIKE '%anv%';
7-2、_
mysql> select prod_id,prod_price,prod_name from products where prod_name LIKE '_ ton anvil';
8、正規表現
likeはすべてにマッチし、REGEXPはすべてと部分にマッチします.
mysql> select prod_name from products where prod_name ='JetPack 1000';
+--------------+
| prod_name |
+--------------+
| JetPack 1000 |
+--------------+
1 row in set (0.00 sec)
mysql> select prod_name from products where prod_name REGEXP '1000';
+--------------+
| prod_name |
+--------------+
| JetPack 1000 |
+--------------+
1 row in set (0.00 sec)
デフォルトでは大文字と小文字を区別せず、大文字と小文字を区別する必要がありますbinary
mysql> select prod_name from products where prod_name REGEXP 'jetpack .000';
mysql> select prod_name from products where prod_name REGEXP binary 'JetPack .000';
10、計算フィールド
mysql> select concat (vend_name , 'C',vend_country,')') from vendors order by vend_name;
+-------------------------------------------+
| concat (vend_name , 'C',vend_country,')') |
+-------------------------------------------+
| ACMECUSA) |
| Anvils R UsCUSA) |
| Furball Inc.CUSA) |
| Jet SetCEngland) |
| Jouets Et OursCFrance) |
| LT SuppliesCUSA) |
+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> select concat (rtrim(vend_name) , 'C',vend_country,')') from vendors order by vend_name;
mysql> select concat (rtrim(vend_name) , '(',rtrim(vend_country),')') as vend_title from vendors order by vend__name;
mysql> select quantity*item_price as expand_price from orderitems where order_num =20005;
11、関数
mysql> select vend_name,upper(vend_name) as ven_name_upcase from vendors order by vend_name;
11-2時間関数
9月のある日のデータを取る
mysql> select cust_id,order_num from orders where Date(order_date) = '2005-09-01';
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
+---------+-----------+
1 row in set (0.00 sec)
9月の全月間の注文を取る
mysql> select cust_id,order_num from orders where Date(order_date) between '2005-09-01' and '2005-09-30';
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
| 10003 | 20006 |
| 10004 | 20007 |
+---------+-----------+
3 rows in set (0.00 sec)
mysql> select cust_id,order_num from orders where Year(order_date) and month(order_date) = 9;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
| 10003 | 20006 |
| 10004 | 20007 |
+---------+-----------+
3 rows in set (0.00 sec)
11-4数値処理関数
11-5集計関数
mysql> select avg(prod_price) as avg_price from products;
データのグループ化
GROUP BY句とHAVING句
mysql> select vend_id,count(*) as num_prods from products group by vend_id;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1001 | 3 |
| 1002 | 2 |
| 1003 | 7 |
| 1005 | 2 |
+---------+-----------+
4 rows in set (0.00 sec)
mysql> select vend_id,count(*) as num_prods from products group by vend_id with rollup;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1001 | 3 |
| 1002 | 2 |
| 1003 | 7 |
| 1005 | 2 |
| NULL | 14 |
+---------+-----------+
5 rows in set (0.00 sec)
having
唯一の違いはWHEREフィルタラインであり、HAVINGフィルタパケットである.WHEREはデータパケット前にフィルタリングし、HAVINGはデータパケット後にフィルタリングする
mysql> select vend_id,count(*) as num_prods from products group by vend_id having count(*)>=2;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1001 | 3 |
| 1002 | 2 |
| 1003 | 7 |
| 1005 | 2 |
+---------+-----------+
4 rows in set (0.00 sec)
mysql> select vend_id,count(*) as num_prods from products where prod_price>=10 group by vend_id having count(*)>=2;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1003 | 4 |
| 1005 | 2 |
+---------+-----------+
2 rows in set (0.00 sec)
mysql> select order_num ,sum(quantity*item_price) as ordertotal from orderitems
-> group by order_num
-> having sum(quantity*item_price) >= 50
-> order by ordertotal;
+-----------+------------+
| order_num | ordertotal |
+-----------+------------+
| 20006 | 55.00 |
| 20008 | 125.00 |
| 20005 | 149.87 |
| 20007 | 1000.00 |
+-----------+------------+
4 rows in set (0.00 sec)
じゅんじょ
12サブクエリ
mysql> select cust_id from orders where order_num in (select order_num from orderitems where prod_id ='TNT2');
+---------+
| cust_id |
+---------+
| 10001 |
| 10004 |
+---------+
15接続テーブル
デカルト積
2つのテーブルを同時にデータソース(from後のテーブル名)として使用し、マッチング条件を追加しない場合、結果セットはディカル積になります.ディカル積の結果は意味がありませんが、ディカル積は連合クエリー、接続クエリーの基礎です.
1.クロスコネクションcross join
表Aの1つのレコードを使用して、表Bにすべてのレコードを接続すると、デカルト積になります.
2.内部接続
selectフィールドリストfromテーブルA【inner】joinテーブルB、一致した成功したレコード
3.外部接続は左接続と右接続に分けられ、
左の接続は左のすべて、右の一致する部分を保持します.
4.usingキーワード
, , using using('cid')
現在のノートは『MySQL必知必会』から