SQL快速入門
簡介
- SQL (Structured Query Language,結構化查詢語言): 與資料庫溝通的語言。用於存取數據以及查詢、更新和管理關係資料庫系統。
參考資料:
SELECT 查詢記錄(建立資料集)
SELECT 欄位 FROM `資料表`
[WHERE 條件]
[LIMIT [起始記錄列數,] 記錄筆數]
[ORDER BY `欄位` ASC/DESC]
SELECT 欄位 FROM `資料表\
- 欄位 >> 資料表(table) >> 資料庫(database)
- * :所有欄位
WHERE 條件
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
LIMIT 限制筆數
ORDER BY 排序
多個條件
JOIN ON 結合
- 只有在兩資料表指定欄位的紀錄都符合結合條件時才會被結合成一筆記錄
- 若結合後該欄位沒有相對應的值則欄位值為NULL
- FROM為主資料表
範例一:
SELECT profiles.Name, class.Hour
FROM `profiles`
INNER JOIN `class` ON profiles.ID = class.Name_ID
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
範例二:
SELECT profiles.Name, class.Hour
FROM `class`
INNER JOIN `profiles` ON profiles.ID = class.Name_ID
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
範例三:
SELECT profiles.Name, class.Hour
FROM `profiles`
LEFT JOIN `class` ON profiles.ID = class.Name_ID
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
範例四:
SELECT profiles.Name, class.Hour
FROM `class`
LEFT JOIN `profiles` ON profiles.ID = class.Name_ID
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
範例五:
找出居住在桃園縣市,缺課時數介於3到6小時(包含)的學生。資料表欄位呈現ID、Name、Addr、Hour
SELECT profiles.ID, profiles.Name, profiles.Addr, class.Hour
FROM `profiles`
INNER JOIN `class` ON profiles.ID = class.Name_ID
WHERE class.Hour BETWEEN 3 AND 6
AND profiles.Addr LIKE '桃園%'
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
INSERT INTO 插入資料
INSERT INTO 資料表 ( 欄位1[ , 欄位2 , 欄位3…] )
VALUES ( 值1 [ , 值2 , 值3…] )
- 欄位與值必須相對應
- 主鍵:唯一值
- AUTO_INCREMENT:自動產生流水編號。
若曾刪除資料,再新增,其編號會自動往後
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
範例:
INSERT INTO profiles (ID, Name, Old, Addr)
VALUES('', '黃禦寒', '26', '台中市')
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
UPDATE SET 更新資料
UPDATE 資料表 SET 欄位1 = 新值 [ , 欄位2 = 新值 … ]
WHERE 條件
- 有時會加上 LIMIT 1 ,為保險起見,避免指令下錯,影響所有資料
範例:
更新地址原台北縣為新北市;原台中縣為台中市;原桃園縣為桃園市
UPDATE `profiles` SET `Addr` = '新北市'
WHERE `Addr` = '台北縣';
UPDATE `profiles` SET `Addr` = '台中市'
WHERE `Addr` = '台中縣';
UPDATE `profiles` SET `Addr` = '桃園市'
WHERE `Addr` = '桃園縣'
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
DELETE FROM 刪除資料
範例:
DELETE FROM `profiles`
WHERE `ID` > 50
LIMIT 1
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →