SELECT
- SELECT * FROM park
- * : 要選取的資料欄
- park : 資料表名稱
- SELECT city FROM park
- SELECT name, established, city FROM park
- SELECT COUNT(*) FROM park
- 只顯示符合條件的資料列數量,不列出資料列
- SELECT SUM(park_visitors) FROM park
- 加總符合條件者的資料欄中的值 (盡量不要用於空值避免出錯)
- SELECT MAX(area_acres) FROM park
- SELECT MIN(area_acres) FROM park
LIMIT
- LIMIT 5
- 限制回傳的資料列數量為5
WHERE
- WHERE type = "national_park"
- 限制搜尋範圍為 type 相符者
- WHERE type != "recreation_area"
AND area_acres > 100000
DISTINCT 排除重複值
- SELECT DISTINCT type FROM park
- SELECT COUNT(DISTINCT type) FROM park
ORDER 排序
- SELECT name FROM park
ORDER BY name - SELECT name FROM park
ORDER BY name DESC - 以遞減方式排序
GROUP 分組
- SELECT type, name FROM park
GROUP BY type
INSERT 新增
INSERT INTO table_name
VALUES (column1, column2, ...)
column 排序與資料表所定義的資料欄順序相符。
例子 :
INSERT INTO park
VALUES (null, 'Googleplex', 'Mountain View', 12, null, 0, '')
此處針對 ID 傳入 null,資料庫有設定只見會自動增加,所以在傳入 null時,系統會自動選擇下一個ID。
UPDATE 更新
語法 :
UPDATE table_name
SET column1 = ...,
column2 = ...,
...
WHERE column_name = ...
...
例子:
UPDATE park
SET area_acres = 46,
established = 1088640000,
type = 'office'
WHERE name = 'Googleplex'
DELETE 刪除
語法 :
DELETE FROM table_name
WHERE <column_name> = ...
0 comments:
張貼留言