# SQL筆記紙條 ### 圖書館找到的SQL筆記紙條 1. 從(FROM)資料庫中的表格內選出(SELECT) SELECT"欄位名"FROM"表格名" ```SQL SELECT 'cID' FROM 'students'; SELECT 'cID' ,'cName' FROM 'students'; ``` 2. 如果想知道這個欄位內有哪些不同的值 ```SQL SELECT DISTINCT 'cSex' FROM 'students'; ``` 3. 如果要顯示所有欄位,可以使用`*`來代表 ```SQL SELECT * FROM 'students'; ``` 4. 想要由`students`資料表中挑出所有男性的資料 ```SQL SELECT * FROM 'students' WHERE 'cSex' = 'M'; ``` 5. 由`students`資料表中找出座號大於5的男性的資料 ```SQL SELECT * FROM 'students' WHERE 'cID' > 5 AND 'cSex' = 'M'; ``` 6. 由`students`資料表中找出座號為1,3,5,7,9的學生資料 ```SQL SELECT * FROM 'students' WHERE 'cID' IN {1,3,5,7,9}; ``` 7. 由`students`資料表中找出出生日期在1987~1988之間的學生資料 ```SQL SELECT * FROM 'students' WHERE 'cBirthay' BETWEEN '1987-01-01' AND '1988-12-31'; ``` 8. 由`students`資料表中找出電話號碼是`0918開頭`的學生資料 ```SQL SELECT * FROM 'students' WHERE 'cPhone' LIKE '0918%' ``` 由`students`資料表中找出三個名字中間第二個字是`志`的學生資料 ```SQL SELECT * FROM 'students' WHERE 'cName' LIKE '%志%' ``` 9. 將`students`資料表中所有同學的生日遞減排序 ```SQL SELECT * FROM 'students' ORDER BY 'cBirthay' DESC /*ASC:遞增排序(由小到大)、DESC:遞減排序(由大到小)*/ ``` 多欄位排序,要將`students`資料表中所有同學的資料以性別遞增、生日遞減排序 ```SQL SELECT * FROM 'students' ORDER BY 'cSex' ASC, 'cBirthay' DESC; ``` 承上題,以性別遞增、生日遞減排序後,只顯示四筆 ```SQL SELECT * FROM 'students' ORDER BY 'cSex' ASC, 'cBirthay' DESC LIMIT 4; ``` 10. 新增資料,在`students`資料表中新增一筆資料 ```SQL INSERT INTO 'students' ('cName','cSex','cBirthday','cEmail','cPhone') VALUES ('李伯恩','M','1981-06-15','born@superstar.com','0929011234'); ``` 11. 更新資料,在`students`資料表中修改一筆資料(修改座號`11`號同學的`身高體重`) ```SQL UPDATE 'students' SET 'cHeight'=174,'cWeight'=92 WHERE 'cID'=11; ``` 12. 刪除資料,在`students`資料表中刪除一筆資料(刪除座號大於`11`號的同學) ```SQL DELETE FROM 'students' WHERE 'cID'>11; ``` 刪除資料,在`students`資料表中刪除一筆資料(刪除座號`11`號的同學) ```SQL DELETE FROM 'students' WHERE 'cID' IN {11}; ```