1. 程式人生 > >Navicat基礎mysql語法

Navicat基礎mysql語法

今天告訴大家一些關於Navicat的一些基礎mysql語法,這個也是我的老師教的,整合到了一起,希望對大家有所幫助。

-- 增,刪,改 insert  delete  update 

-- 增  必須向所有列填充資料,除了(自增列,有預設值列,允許為空)可以不填充
INSERT [INTO] 表(列列表) values (值列表)

-- 刪 
 DELETE from 表[where 條件]
DELETE from student

-- 改
UPDATE 表 set 列 = 值,列 = 值 [where 條件]
update student set name = '張亮',set  sex = '女' where studentno = '4'

-- 查詢 模糊查詢  分頁  
like between in is null 


-- 查詢  排序  分組  連線
-- 排序 order by 預設是升序:asc  降序:desc
-- 按多個列來排序,先按第一個欄位排序,在此基礎上再按第二個欄位進行排序.
select * from student order by age,studentno
-- 分組 聚合函式 sum avg max min count
select sum(age),avg(age),max(age),min(age) from student;
-- count 是統計有多少資料行,如果是統計某個列,則會忽略列中的NULL值。
select count(email) from student
-- 統計有多少學生沒有錄入郵箱資訊??
select count(*) from student where email is null


-- 分組,group by  是把資料進行分類再彙總,必須要配合聚合函式使用,
-- 關鍵點:按什麼進行分組,用什麼聚合函式進行統計。
-- 如果某個列出現在from關鍵字前,且沒有包含在聚合函式中,則此列必須出現在group by 子句中
-- 統計每個年級有多少學生? 
select gradeId,count(*) from student group by gradeId
-- 統計每個年級男女學生各有多少?  按年級和性別進行分組,用count函式
select gradeid,sex,count(*) from student group by sex,gradeId; 
-- 統計每個年級有多少課時?
select gradeid,sum(classHours) from subject group by gradeid
-- 統計每個年級有多少課程?
select gradeid,count(*) from subject group by gradeid
-- 統計每個學生的總成績和平均成績?
select studentno,sum(result),avg(result) from score group by studentno


-- 連線查詢 內連線 外連線 交叉連線
-- 當資料來自兩個或兩個以上的表時,則才用連線查詢來實現。
-- where 條件是兩個表的主鍵列相等。
select * from student s,grade g where s.gradeid=g.gradeid
-- 建議使用下面的寫法,效能好一些。
select * from student s inner join grade g on s.gradeid=g.gradeid
-- 查詢姓名,學號、課程名、分數  資料來自於3個表?
select name,s.studentno,subjectname,result from student s 
  inner join score c on s.studentno = c.studentno
  inner join subject j on c.subjectno= j.subjectno  


-- 外連線  左外連線  右外連線
/* 左外連線,在前面的表是主表,後面的表是子表,主表的資料全部顯示,
  再用子表的資料進行填充,如果子表中沒有對應的資料,則用NULL來填充 */
select * from student s
  left join score c on s.studentno = c.studentno


-- 查詢有哪些學生沒有參加過考試,用左外連線實現??
select * from student s
  left join score c on s.studentno = c.studentno 
  where c.studentno is null
-- 查詢哪些學生沒有參加考試,用子查詢實現??
-- 子查詢的結果只能是返回一列值,返回的值如果有多個,就只能用in 不能用 = 
select * from student where studentno 
 not in( select studentno from score)