sql的編碼顯得越來越重要,原因是很多邏輯處理,都可以跟sql掛鉤。 個人排斥將邏輯層**複雜化, 因此sql在未來的專案架構中,舉足輕重, 特地開一章節, 來總結一下以往專案中sql的精髓部分。
顯示某個店鋪的任務列表,同時把某個使用者的任務完成情況列出來。 傳入使用者的id_user值, 看看該使用者是否完成任務, 先前寫的sql如下:
select
id_shop,
id_task,
is_online_task,
type_task,
freq,
url,
score,
descripe,
title,
time_start,
time_end
from shop_task t
where t.id_shop = #
and t.type_task =#
order by time_start desc
limit $, $
這個sql是完全在一張表裡面做的, 現在要將使用者的情況加入,使用者完成乙個任務,相應的積分有增加, 這個表叫做score_user表, 為了滿足需求,sql是這樣設計的:
增加乙個任務是否完成的屬性status_finish, 需記住:
(left join是乙個表,牢記這點就好):
select
t.id_shop,
t.id_task,
t.is_online_task,
t.type_task,
t.freq,
t.url,
t.score,
t.descripe,
t.title,
t.time_start,
t.time_end,
uu.status_finish
from shop_task t
left join (select
count(1) as status_finish,
mark, id_user,type_score, time_get
from score_user su
where su.id_user='519006470'
group by su.mark) uu
on t.id_task = uu.mark
where t.id_shop = '50001020'
order by time_start desc
由於left join 存在多個數量時, 返回內容過多,需要調整一下:
select
t.id_shop,
t.id_task,
t.is_online_task,
t.type_task,
t.freq,
t.url,
t.score,
t.descripe,
t.title,
t.time_start,
t.time_end,
uu.time_get,
uu.id_user
from shop_task t
left join (select
mark, id_user,type_score, time_get
from score_user su
where su.id_user='340001477'
and type_score = 6
order by time_get desc) uu
on t.id_task = uu.mark
where t.id_shop = '50001020'
order by time_start desc
由於left join 存在多個數量時, 返回內容過多,需要調整一下:
select
t.id_shop,
t.id_task,
t.is_online_task,
t.type_task,
t.freq,
t.url,
t.score,
t.descripe,
t.title,
t.time_start,
t.time_end,
uu.time_get,
uu.id_user
from shop_task t
left join (select
mark, id_user,type_score, time_get
from score_user su
where su.id_user='519006470'
and type_score = 6
order by time_get desc
limit 0,1) uu
on t.id_task = uu.mark
where t.id_shop = '50001020'
order by time_start desc
limit將導致內容過少… ⊙﹏⊙b汗!
select
mark, id_user,type_score, time_get
from score_user su
where su.id_user='519006470'
and type_score = 6
order by time_get desc
ok終於可以搞定,group
是個很強大的語法。
select
mark, id_user, type_score, max(time_get)
from score_user su
where id_user='519006470'
and type_score = 6
group by mark
結合以上,我們的結論是:
select
t.id_shop,
t.id_task,
t.is_online_task,
t.type_task,
t.freq,
t.url,
t.score,
t.descripe,
t.title,
t.time_start,
t.time_end,
uu.last_time,
uu.id_user
from shop_task t
left join (select
mark, id_user, type_score, max(time_get) as last_time
from score_user su
where id_user='519006470'
and type_score = 6
group by mark) uu
on t.id_task = uu.mark
where t.id_shop = '50001020'
order by time_start desc
總結:
1. left joion即增加乙個表
2. 去掉笛卡爾常量的方法, 加限定詞: max, group by等
Oracle動態SQL的拼裝要領
oracle的動態sql語句用起來很方便,但其拼裝過程太煩人。尤其在拼裝語句中涉及到date型別欄位時,拼裝時要加to char先轉換成字元,到了sql中又要使用to date轉成date型別和原字段再比較。www.akdy.cn 例如這樣乙個sql select and t.created to ...
xlsxwriter編寫複雜表頭
作為一名資料分析小白,並且第一次寫blog,如果有錯,請多多指教。並且 可能太手工了,如果有更高效 簡易的寫法,請指點一下。先上圖,做成的效果大概就這個樣子,在excel中,如果要做成這種圖,很簡單,就是合併單元格的事。但在python裡面寫,就難倒我了。看完資料後,發現有兩個方向可以做成複雜表頭。...
sql文的編寫
define sql final string str sql state select count t137.kanri no1 from twmt137 t137,twmt138 t138 null ownbean.gettwmct11901bean getstrsitukacd twmt118...