# Mysql 語法 ## 登入mysql * 連線資料庫 ``` mysql -h 127.0.0.1 -P 3306 -uroot -p123456 -h為遠端IP,-P為埠號,-u為使用者名稱,-p為密碼 ``` ## 資料庫新增刪除 1. 新增資料庫 ``` $create database 資料庫名稱; ``` 2. 刪除資料庫 ``` $drop database 資料庫名稱; ``` 3. 切換資料庫 ``` $use database 名稱; ``` 4. 查看現有資料庫 ``` $show database; ``` ## 資料表 新增/刪除 1. 建立資料表 ``` $create table 資料表名稱; ``` 2. 刪除資料表 ``` $drop table 資料表名稱; ``` ## 資料表欄位 新增/刪除/設定 1. 新增資料表欄位 ``` $create table 資料表名稱(id int primary key, name varchar(20) not null, hero_level char(1)); (欄位 型態 主鍵 是否可為空值) ``` * 新增欄位到舊資料表 ``` $alter table 資料庫名稱.資料表名稱 add column 新增欄位名 型別(char) after x; //新增到 x欄位後面 //first 新增到最前面 ``` 2. 刪除資料表內的欄位 ``` $alter table 資料庫名稱.資料表名稱 drop colume 刪除的欄位名稱; ``` 3. 改變欄位名稱、型別 * 更改欄位名稱 ``` $alter table '資料庫名稱' '資料表名稱' change column '原本欄位名' '要改的欄位名' char(1); EX. 'A01' 'B01' //將欄位A01改成B01 ``` * 更改欄位型別 ``` $alter table '資料庫名稱'.'資料表名稱' modfily 更改的欄位名 更改型別; EX. descrip varchar(20); ``` ## 新增/更新/刪除 欄位內的資料 1. 新增 資料 ``` $insert into '資料庫名稱'.'資料表名稱'('新增欄位','新增欄位','新增欄位'...) value ('資料','資料','資料',...) ``` 3. 更新資料 ``` $update 資料表名稱 set 欄位名稱 = 更新的資料,欄位名稱 = 更新的資料,欄位名稱 = 更新的資料, where 要更新欄位名稱; ------------------------------------------- EX. 將A班 小明 age改為25, 人改為動物 $update A班 set age = 25, 人 = '動物' //char 需加'' where name = "小明"; ``` * 一次更改多筆資料 更改多筆資料,需將安全模式關閉下使用 ``` $set sql_safe_update = 0; //關閉為0, 預設為開啟1 使用後請記得再次開啟 ``` ``` $update 資料表名稱 set 改變欄位 = 要改變數值; 將A班 所有人 age+1 EX. $update A班 set age = age + 1; ``` 2. 刪除資料 ``` $delete from 資料表名稱 where 欄位名稱 = 欲刪除資料 EX. 將 A班 id欄位為 3 的人刪除 $delete from A班 where id = 3; ``` * 一次刪除多筆 刪除多筆資料需將安全模式關閉 ``` $delete from 資料表名稱 where 欄位名稱 = 符合欄位名稱值; EX. 將A班 性別為男生 刪除 $delete from A班 where 性別 = 男生; ``` # Mysql查詢語法 ## 基本查詢,將資料表內資料全部顯示 ``` $selet * // * 代表全部欄位 from 資料庫名稱.資料表名稱; ``` * 加入條件查詢 ``` $select * from 資料庫名稱.資料表名稱 where 欄位名稱 = 條件 and 欄位名稱 = 條件 .... ``` * 僅列出其中幾個符合條件的欄位資料 ``` $select 欄位名稱, 欄位名稱 from 資料庫名稱.資料表名稱 where 欄位名稱 = 條件 and 欄位名稱 = 條件...; EX. 列出A班資料表內,符合性別=男 的身高/體重資料 $select 身高, 體重 from 班級資料庫.A班 where 性別 = 男; ``` ## 查詢某欄位內為空值的資料 ``` $select * from 資料庫名稱.資料表名稱 where 欄位名稱 is null; ``` ## 查詢資料內符合條件的資料 ``` $select * //查詢資料內含有XX的資料 from 資料庫名稱.資料表名稱 //%為指定前後是否能有其他字元 where 欄位名稱 like '%XX%'; //'%xx' xx前面可有字,後面不可 //'xx%' 反之 當查詢型別為text時,需將''改成"" ``` * 查詢條件為範圍時 ``` 方法一 $select * from 資料表名稱 where 欄位名稱 >= 10 and 欄位名稱 <= 25; 方法二 $select * from 資料表名稱 where 欄位名稱 between 10 and 25; ``` * 查詢X or Y 其中一項即可OR ``` 方法一 $select * from 資料表名稱 where 欄位名稱 = 'X' or 欄位名稱 = 'Y'; 續加時 or ..... 方法二 $select * from 資料表名稱 where 欄位名稱 in ('X','Y'); ``` * 查詢不是X的 ``` $select * from 資料表名稱 where 欄位名稱 <> 'X'; ``` * 查詢不是 X 也不是 Y ``` $select * from 資料表名稱 where 欄位名稱 <> 'X' and 欄位名稱 <> 'Y'; 也可寫成 where 欄位名稱 not in ('S', 'A'); ``` # 進階查詢 1. 計算符合條件的欄位總數 ``` $select count(*) from 資料表名稱 where 欄位名稱 = '條件' ``` EX. 計算A級英雄個數 ``` select count(*) from heroes where hero_level = 'A'; ``` 2. 計算符合條件的欄位內數值 加總/平均 ``` $select sum(欲計算欄位名稱) \\計算平均時,將sum改為avg from 資料表名稱 where 欄位名稱 = '條件' and 欄位名稱 = '條件'...; ``` EX 計算A級英雄年紀加總 ``` $select sum(age) from heroes where hero_level = 'A' and age is not null; ``` 3. 找出欄位內資料的最 大/小 值 ``` $select max(欄位名稱) \\最小值則改為min from 資料表名稱; ``` 4. 分組計算 列出英雄各年紀分組並計算總合 ``` $select hero_level, sum(age) from heroes group by heor_level; ``` 列出怪物資料表,危險等級的各組 ``` $selet distinct danger_level from monster; ``` ## 排序 1. 列出符合條件的某欄位 排序 ``` select * from 資料表名稱 where 欄位名稱 = '條件' oredr by 欲排序的欄位名稱 (desc) //小至大為預設值 //大至小則加入 desc ``` * 列出符合條件的欄位並排序出最 前/後 X位 ``` select * from 資料表名稱 where 欄位 = '條件' order by 欲排序的欄位名稱 (desc) limit X; ``` ## 查詢關聯性資料表 ![](https://i.imgur.com/jYBG4Fw.png) EX 查詢A,B兩堂課同時存在的學生 ``` $select * from A, B where A.name = b.name; ``` ![](https://i.imgur.com/2ilDK9P.png) EX 查詢反派是被誰打倒 ``` $select monster.name, monster.dannger_level, heros. from monster left join heroes on monsters.kill_by = heroes on monsters.kill_by in not null; ``` ## 縮寫 EX ``` $select monster.name, monsters.dannger_level, heroes.name from monsters as m //將m代表monsters left join heroes as h //h = heroes on m.kill_by = h.id where m.kill_by is not null; ```