




INSERT INTO `表格名`(`欄位1`,`欄位2`,...)VALUES('值1','值2',...);
INSERT INTO `message` ( `time` , `message` ) VALUES ( NOW( ) ,'Hello message!' )
INSERT INTO `message` (
`time` ,
`message`
)
VALUES (
NOW( ) , 'Hello message \'A\' !'
), (
NOW( ) , 'Hello message \'B\' !'
)
UPDATE`表格名`SET`欄位1` = 新值1,`欄位2` = 新值2WHERE‵欄位3‵ = 1;
UPDATE`message`SET`time` = NOW(),`message` = '修改留言!!'WHERE`index`=1;
DELETE FROM‵表格名‵ WHERE{條件};
DELETE FROM
`message`
WHERE
`index`=1;SELECT`欄位1`,`欄位2`,...FROM"表格名";
SELECT*FROM`message`
SELECT*FROM`message`WHERE`index` = 3
SELECT*FROM`message`WHERE`index` > 1AND `time` >= '2013-07-22 19:45:00'
SELECT*FROM`message`WHERE`index` > 1AND `time` => '2013-07-22 19:45:00'ORDER BY`index` DESC
ORDER BY `欄位一` [ASC / DESC], `欄位二` [ASC / DESC]SELECTCOUNT(`message`)FROM`message`
SELECT*,count(*)FROM`message`GROUP BY`time`
SELECT*,count(*)FROM`message`GROUP BY`time` HAVING count(*) > 3
SELECTCOUNT(`message`) AS `C`FROM`message`














SELECT
`資料表1`.`欄位2`,
`資料表2`.`欄位3`,
`資料表2`.`欄位4`
FROM
`資料表1`
Inner Join
`資料表2`
ON
/* ON後面接 {聯集條件} */
`資料表1`.`欄位1` = `資料表2`.`欄位2`
AND `資料表1`.`欄位2` = `資料表2`.`欄位3`
SELECT
student.student_name,
score.score,
score.`subject`
FROM
student
Inner Join
score
ON
student.student_id = score.student_id
SELECT
`學生`.`student_id` AS `編號`,
`學生`.`student_name` AS `名字`,
`成績`.`subject` AS `科目`,
`成績`.`score` AS `得分`
FROM
`student` AS `學生`
Inner Join
`score` AS `成績`
ON
`學生`.`student_id` = `成績`.`student_id`





SELECT
`學生`.`student_id` AS `編號`,
`學生`.`student_name` AS `名字`,
`科目`.`subject` AS `科目`,
`成績`.`score` AS `得分`
FROM
`student` AS `學生`
Inner Join
`score` AS `成績`
ON
`學生`.`student_id` = `成績`.`student_id`
Inner Join
`subject` AS `科目`
ON
`成績`.`subject` = `科目`.`subject_id`

SELECT
*
FROM
`TABLE_A` AS `LEFT_TABLE`
Left Join
`TABLE_B` AS `RIGHT_TABLE`
ON
`LEFT_TABLE`.`student_id` = `RIGHT_TABLE`.`student_id`
SELECT `學生`.`student_id` AS `編號`, `學生`.`student_name` AS `名字`, `性別`.‵sex‵ AS `性別`, `科目`.`subject` AS `科目`, `成績`.`score` AS `得分`FROM `student` AS `學生` Inner Join `score` AS `成績` ON `學生`.`student_id` = `成績`.`student_id` Inner Join `subject` AS `科目` ON `成績`.`subject` = `科目`.`subject_id` Left Join `sex` AS `性別` ON `性別`.sex_id = `學生`.`sex_id`

SELECT `學生`.`student_id` AS `編號`, `學生`.`student_name` AS `名字`, IF(`性別`.sex Is Null,'未輸入',`性別`.sex ) AS `性別`, `科目`.`subject` AS `科目`, `成績`.`score` AS `得分`FROM `student` AS `學生` Inner Join `score` AS `成績` ON `學生`.`student_id` = `成績`.`student_id` Inner Join `subject` AS `科目` ON `成績`.`subject` = `科目`.`subject_id` Left Join `sex` AS `性別` ON `性別`.sex_id = `學生`.`sex_id`

SELECT{要查詢的欄位} FROM{要查詢的資料表} WHERE{查詢條件} GROUP BY{以...為群組} HAVING{函數條件} ORDER BY{以...欄位排序} LIMIT開始位置,查詢數量

SELECT `學生`.`student_id` AS `編號`, `學生`.`student_name` AS `名字`, IF(`性別`.sex Is Null,'未輸入',`性別`.sex ) AS `性別`, `科目`.`subject` AS `科目`, `成績`.`score` AS `得分`FROM `student` AS `學生` Inner Join `score` AS `成績`ON `學生`.`student_id` =`成績`.`student_id` Inner Join `subject` AS `科目` ON `成績`.`subject` = `科目`.`subject_id` Left Join `sex` AS `性別` ON `性別`.sex_id = `學生`.`sex_id` WHERE `成績`.‵score‵>(SELECT AVG(‵score`.`score`) FROM `score` )
SELECT `學生`.`student_id` AS `編號`, `學生`.`student_name` AS `名字`, IF(`性別`.sex Is Null,'未輸入',`性別`.sex ) AS `性別`, `科目`.`subject` AS `科目`, `成績`.`score` AS `得分`, IF(`成績`.`score` = (SELECT MAX(‵score`.`score`) FROM `score` WHERE `學生`.`student_id` =`score`.`student_id` ),'是','否') AS `是否為最高分科目`FROM `student` AS `學生` Inner Join `score` AS `成績`ON `學生`.`student_id` =`成績`.`student_id` Inner Join `subject` AS `科目` ON `成績`.`subject` = `科目`.`subject_id` Left Join `sex` AS `性別` ON `性別`.sex_id = `學生`.`sex_id`

SELECT `學生`.`student_id` AS `編號`, `學生`.`student_name` AS `名字`, IF(`性別`.sex Is Null,'未輸入',`性別`.sex ) AS `性別`, `科目`.`subject` AS `科目`, `成績`.`score` AS `得分`FROM `student` AS `學生` Inner Join `score` AS `成績` ON `學生`.`student_id` = `成績`.`student_id` Inner Join `subject` AS `科目` ON `成績`.`subject` = `科目`.`subject_id` Left Join `sex` AS `性別` ON `性別`.sex_id = `學生`.`sex_id`
select
`student_id` AS `student_id`,
max(`score`) AS `MAX`
from
`score`
group by
`student_id`
SELECT `學生`.`student_id` AS `編號`, `學生`.`student_name` AS `名字`, IF(`性別`.sex Is Null,'未輸入',`性別`.sex ) AS `性別`, `科目`.`subject` AS `科目`, `成績`.`score` AS `得分` IF(`成績`.`score` =`最高分科目分數`.`MAX`,'是','否') AS `是否為最高分科目`FROM `student` AS `學生` Inner Join `score` AS `成績` ON `學生`.`student_id` =`成績`.`student_id` Inner Join `subject` AS `科目` ON `成績`.`subject` = `科目`.`subject_id` Left Join `sex` AS `性別` ON `性別`.sex_id = `學生`.`sex_id` Inner Join `student_max_score` AS `最高分科目分數` ON `最高分科目分數`.`student_id` = `學生`.`student_id`