標籤:複製 理解 color upd 多列 max each 一點 tar
#1 增(一共有三種方式) # 插入單條記錄 insert into t1(name,...) values(‘lzp‘,..); 注意一點:t1(name,...)必須包含所有非空列(除去自增列) # 插入多條記錄 insert into t1(name,age) values(‘lzp‘,12),(‘alex‘,30); # 拷貝複製(拷貝t2表的資料到t1) insert into t1(name,age) values select name,age from t2;#2 改 update t1 set name=‘b‘,age=13 where name=‘lzp‘; update t1 set name=‘c‘ where name=‘alex‘;#3 查(重點) select [...] from tb [...] 從前面[]擴充: select name as cname, age from tb1; # 起別名 select name, age, 1 from tb1; # 多顯示一列,該列的值為常量 select name, age, variable from tb1; # 多顯示一列,該列的值為變數,變數的定義在前面實現 從後面的[]擴充: 1 where select name, age from tb1 where id in (2,4,6) # id =2 或id=4,或id=6 集合(2,4,6) select name, age from tb1 where id <> 3 # <>不等於(小於<或者大於>) select name, age from tb1 where id not in (2,4,6) select name, age from tb1 where id between 5 and 12; # id in [5,12] #閉區間集合[5,12] select name, age from tb1 where id in (select id from tb2); # 萬用字元%(多個任一字元), 底線_(一個任一字元) select name, age from tb1 where name like "a%" # 以a開頭的名字 note: 不能允許這種 where id =1 and id=2, 這樣查到的肯定是Null 2 分頁limit [start], count ; start 是從0開始,預設值=0;count表示查看的記錄數 select name, age from tb1 limit 10; #查看第一條資料到第10條資料; select name, age from tb1 limit 10,10; #查看第11條資料到第20條資料 簡單分頁: page = int(input(‘‘).strip()) start = (page-1)*10 select name, age from tb1 limit start,10 limit 10 offset=20; 等效 3 order by 排序 order by 列名 [asc|desc] # 列名都是整數 select name, age from tb1 order by id desc;# 按id從大到小排序 select name, age from tb1 order by id desc limit 0,2; # 多列排序 order by cow_1 desc, cow_2 desc # 注意不是order by cow_1, order by cow_2 先按cow_1降序排,如果cow_1有相同的記錄,對cow_1相同的記錄按cow_2進行降序排 4 分組group by + 彙總函式 group by part_id: 可以理解成,將part_id相同的折成一條記錄(怎麼折?去重(最好不用distinct) 對於相同的記錄,可以根據函數count(),max(),min(),sum(),avg()折成一條記錄 這些函數稱為彙總函式 example1: 使用者表,部門表,查看每個部門有多少人 select part_id, count(id), max(id) from tb1 group by part_id; 5 分組group by + 彙總函式 + 二次刷選(having) 對彙總函式結果進行二次刷選時,必須用having select part_id, count(id) from tb1 group by part_id having count(id) > 1 # 刷選出部門人數大於1的部門 6 連表操作 將所有表select出來,然後告訴他們的關係 select * from user, department where user.part_id = department.id 推薦:left join ...on (之前版本效能更好,目前版本兩者效能是一致) #將所有表select 出來; select * from tb1 left join tb2 # on tb1.part_id = tb2.id 告訴他們表之間的關係 select * from user left join department on user.part_id = department.id 7 連表操作left join 和right join, inner join 區別 select * from tb1 left join tb2 where ... tb1會全部顯示,左邊的表會全部顯示; inner join 等效於將實現left join連表,後將Null出現的整行隱藏(不顯示) 8 多張表串連 select * from tb1 left join tb2 where ... # 此時只能告訴tb1和tb2的關係 left join tb3 where ... # 此時可以告訴tb1和tb3的關係,tb2和tb3的關係(此時,已經將tb1,tb2,tb3關聯進來了) 連表的時候,如果遇到列名相同,*可變成表名. select tb1.id, tb2.id, tb3.id from tb1 left join tb2 where .. left join tb3 where .. 舉例: http://images2015.cnblogs.com/blog/425762/201608/425762-20160803224643778-2071849037.png http://www.cnblogs.com/wupeiqi/articles/5729934.html 學生,班級,老師,課程,成績表: 學生表:foreign key 班級表 班級表: 課程表:foreign key 老師 成績表:學生id, 課程id, 分數; foreign key 學生表, foreign key 課程表 #9 暫存資料表 將查詢得到的資料,作為一個暫存資料表;在記憶體中儲存,但並沒有寫到硬碟上。 文法: (select * from tb1 where age > 20) as B; select sid from(select * from tb1 where age > 20) as B; #10 增加顯示列(前提:select score from tb1 where name="yuwen"只能是一個值) select id, (select score from tb1 where name="yuwen") from tb2; tb1和tb2不是同一表,增加的顯示列不是處理同一個表同一行的資料 #11 增加顯示列 select id, (select score from tb1 as s2 where s2.id= s1.id) as score from tb1 as s1 # 最外層的迴圈,一行一行資料 # 裡面的迴圈,每一行中的一列一列 #12 條件陳述式 case when min(num) < 10 then 0 else 1 end as c #13 mysql的三元運算 if(True, 1,0) # 計算課程平均分從高到底顯示,顯示任課老師 avg(if(isnull(score.num), 0, score.num)) # 因為為空白的時候無法計算 select score.course_id, course.cname, teacher.tname, avg(if(isnull(score.num), 0, score.num)) as average from score left join course on score.course_id=course.cid left join teacher on course.teacher_id=teacher.tid group by score.course_id order by average desc; # 此時可以用average,但是在select裡面不能用average #14 雙重迴圈 select * from (select A.student_id, A.course_id, A.num, (select B.num from score as B where B.course_id = A.course_id order by B.num desc limit 0,1) as frist_s, (select B.num from score as B where B.course_id = A.course_id order by B.num desc limit 1,1) as second_s from score as A) as C where C.num >= C.second_s #15 多表操作,直接用連表,會比較好理解和設定where #16 # 查詢沒有學過李平老師課學生的姓名,id---沒有學過李平老師任何一門課的學生 先刷選出選過李平老師任意一門課的學生, # 碰到Not,一定要在最頂一層進行Not, 在學生表裡面not in (學過老師課程學生的id,還有進行分組過濾掉重複的) # 先查到李平老師教過哪些課; -- select student.sid,student.sname from student where student.sid not in ( -- select student_id from score -- where score.course_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname=‘李平老師‘) -- group by student_id); #17 where group by having : 先進行where一次刷選,再進行group having刷選 #18 union 上下連表(union自動去重,union all則不會去重) select id, name from tb1 union select sid, sname frou stb1;
[oldboy-django][2深入django]mysql查詢語句--原生sql