1. 程式人生 > 資料庫 >Mysql學習隨筆--8

Mysql學習隨筆--8

分頁:
select * from tb12 limit 10;
select * from tb12 limit 0,10;
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;
select * from tb12 limit 10 offset  20;

# page = input('請輸入要檢視的頁碼')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10; 1
# select * from tb12 limit 10,10;2

排序:
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc(優先),id desc;

取後10條資料
select * from tb12 order by id desc  limit 10;

select count(id) from userinfo5;【取數量】

分組:
select count(id),max(id),part_id from userinfo5 group by part_id;

count
max
min
sum
avg

 

 

**** 如果對於聚合函式結果進行二次篩選時?必須使用having ****
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;

連表操作:[兩表之間有一定的關係]

select * from userinfo5,department5

select * from userinfo5,department5 where userinfo5.part_id = department5.id

select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
select * from department5 left join userinfo5 on userinfo5.part_id = department5.id
# userinfo5左邊全部顯示[記住 left on  就好]

# select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
# department5右邊全部顯示


select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
將出現null時一行隱藏

多表相連【★】
select  *  from
department5
left join userinfo5 on userinfo5.part_id = department5.id
left join userinfo6 on userinfo5.part_id = department5.id

select
score.sid,
student.sid
from
score

left join student on score.student_id = student.sid

left join course on score.course_id = course.cid

left join class on student.class_id = class.cid

left join teacher on course.teacher_id=teacher.tid