MySQL 基本語法 (五) SELECT

閱讀時間約 1 分鐘

SELECT 取得資料


原始`student` TABLE :

  • 取得所有屬性
SELECT * FROM `student`;
raw-image
  • 取得name這個屬性
SELECT `name` FROM `student`;
raw-image
  • 取得多個屬性
SELECT `name`,`major` FROM `student`;
raw-image
  • ORDER BY 取得資料時,一起做排序

ASC (預設)由小到大

SELECT * 
FROM `student`
ORDER BY `score`;
raw-image

DESC 由大到小

SELECT * 
FROM `student`
ORDER BY `score` DESC;
raw-image
  • ORDER BY後面也可以接多個屬性

先根據score屬性做排序,如果score屬性的值一樣,接下來會根據student_id排序

SELECT * 
FROM `student`
ORDER BY `score`, `student_id`;
  • LIMIT 限制回傳資料的筆數

回傳前3筆資料

SELECT * 
FROM `student`
LIMIT 3;
raw-image
  • 先做排序,再取前n筆資料
SELECT * 
FROM `student`
ORDER BY `score`
LIMIT 4;
raw-image
  • 條件判斷WHERE
SELECT * 
FROM `student`
WHERE `major` = '英語' AND `student_id` = 1;
raw-image
  • WHERE ... IN ...


SELECT * 
FROM `student`
WHERE `major` IN ('歷史','英語','生物');
# 等於下列
# WHERE `major`='歷史' OR `major` = ​'英語' OR `major` = '生物';
raw-image


7會員
36內容數
這裡會放一些我寫過的 Leetcode 解題和學習新技術的筆記
留言0
查看全部
發表第一個留言支持創作者!