Skip to main content
Version: v4

3.2 基礎 SELECT 語法教學

NULL 排序在最前

SELECT *
FROM table_name
ORDER BY column1 ASC NULLS FIRST

NULL 排序在最後

SELECT *
FROM table_name
ORDER BY column1 ASC NULLS LAST

GROUP BY

依照特定欄位分組

SELECT *
FROM table_name
GROUP BY column1

依照特定欄位分組

SELECT count(*)
FROM table_name
GROUP BY column1

HAVING

篩選分組過後的資料。

SELECT count(column1), column2
FROM table_name
GROUP BY column2
HAVING count(column1) > value1

JOIN

依照特定欄位組合兩張表

SELECT *
FROM table1
JOIN table2
ON table1.column1 = table2.column1

UNION[​]

合併兩張表

SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;

LIMIT, OFFSET

  • LIMIT number 最多取得幾筆資料
  • OFFSET number 忽略前幾筆資料

取得第 5 筆之後的 10 筆資料

SELECT *
FROM table_name
OFFSET 5
LIMIT 10