Mysql的收穫

來源:互聯網
上載者:User

標籤:

今天公司的業務需求中,遇到一個很鬱悶的情況。

有一個申請記錄,缺少申請原因的欄位。

我以為很簡單,採用left jion 很容易擷取到申請原因。

SELECT a.*,b.RealName,c.DeptName,d.FlowApplyNote as n1,e.FlowApplyNote as n2, f.FlowApplyNote as n3 ,g.FlowApplyNote as n4, h.FlowApplyNote as n5 FROM `AttApplyRecords` as a LEFT JOIN `SysUsers` as b ON a.FlowApplyUserId =
b.UserId LEFT JOIN `SysDepts` as c ON c.DeptId = b.DeptId LEFT JOIN `FlowApplyLeaves` as d ON a.FlowApplyId = d.FlowApplyId and a.FlowId = d.FlowId and d.FlowApplyNote is not null LEFT JOIN `FlowApplyOffDays` as e ON a.FlowApplyId = e.FlowApplyId and a.FlowId = e.FlowId and e.FlowApplyNote is not null LEFT JOIN `FlowApplyNotAtts` as f ON a.FlowApplyId = f.FlowApplyId and a.FlowId = f.FlowId and f.FlowApplyNote is not null LEFT JOIN `FlowApplyOverTimes` as g ON a.FlowApplyId = g.FlowApplyId and a.FlowId = g.FlowId and g.FlowApplyNote is not null LEFT JOIN `FlowApplyTravels` as h ON a.FlowApplyId = h.FlowApplyId and a.FlowId = h.FlowId and h.FlowApplyNote is not null WHERE a.ApplyStatus = 2 ORDER BY b.RealName DESC

 

產生的結果如下

 

後面的n1 n2 n3 n4 n5 不好取。他們應該是申請理由。

於是想到合并欄位。mysql中合并可以採用concat 。但是這要求欄位不能為Null,否則直接返回Null

於是又抓取mysql的 ifNULL函數。成功實現上門的效果。

於是產生下面的是sql語句

 

SELECT a.*,b.RealName,c.DeptName,IFNULL(d.FlowApplyNote,‘‘) as n1, IFNULL(e.FlowApplyNote,‘‘) as n2, IFNULL(f.FlowApplyNote,‘‘) as n3 , IFNULL(g.FlowApplyNote,‘‘) as n4, IFNULL(h.FlowApplyNote,‘‘) as n5,concat(n1,n2,n3,n4,n5) as n FROM `AttApplyRecords` as a LEFT JOIN `SysUsers` as b ON a.FlowApplyUserId = 
b.UserId LEFT JOIN `SysDepts` as c ON c.DeptId = b.DeptId LEFT JOIN `FlowApplyLeaves` as d ON a.FlowApplyId = d.FlowApplyId and a.FlowId = d.FlowId and d.FlowApplyNote is not null LEFT JOIN `FlowApplyOffDays` as e ON a.FlowApplyId = e.FlowApplyId and a.FlowId = e.FlowId and e.FlowApplyNote is not null LEFT JOIN `FlowApplyNotAtts` as f ON a.FlowApplyId = f.FlowApplyId and a.FlowId = f.FlowId and f.FlowApplyNote is not null LEFT JOIN `FlowApplyOverTimes` as g ON a.FlowApplyId = g.FlowApplyId and a.FlowId = g.FlowId and g.FlowApplyNote is not null LEFT JOIN `FlowApplyTravels` as h ON a.FlowApplyId = h.FlowApplyId and a.FlowId = h.FlowId and h.FlowApplyNote is not null WHERE a.ApplyStatus = 2 ORDER BY b.RealName DESC

 

但是提示錯誤。

 

困擾了一會,又想到Mysql 的二次查詢。

 

於是最後 的sql語句是

 

select *,concat(t.n1,t.n2,t.n3,t.n4,t.n5) as FlowApplyNote from (SELECT a.*,b.RealName,c.DeptName,IFNULL(d.FlowApplyNote,‘‘) as n1, IFNULL(e.FlowApplyNote,‘‘) as n2, IFNULL(f.FlowApplyNote,‘‘) as n3 , IFNULL(g.FlowApplyNote,‘‘) as n4, IFNULL(h.FlowApplyNote,‘‘) as n5 FROM `AttApplyRecords` as a LEFT JOIN `SysUsers` as b ON a.FlowApplyUserId =
b.UserId LEFT JOIN `SysDepts` as c ON c.DeptId = b.DeptId LEFT JOIN `FlowApplyLeaves` as d ON a.FlowApplyId = d.FlowApplyId and a.FlowId = d.FlowId and d.FlowApplyNote is not null LEFT JOIN `FlowApplyOffDays` as e ON a.FlowApplyId = e.FlowApplyId and a.FlowId = e.FlowId and e.FlowApplyNote is not null LEFT JOIN `FlowApplyNotAtts` as f ON a.FlowApplyId = f.FlowApplyId and a.FlowId = f.FlowId and f.FlowApplyNote is not null LEFT JOIN `FlowApplyOverTimes` as g ON a.FlowApplyId = g.FlowApplyId and a.FlowId = g.FlowId and g.FlowApplyNote is not null LEFT JOIN `FlowApplyTravels` as h ON a.FlowApplyId = h.FlowApplyId and a.FlowId = h.FlowId and h.FlowApplyNote is not null WHERE a.ApplyStatus = 2 ORDER BY b.RealName DESC ) t

 

經過這些折騰,對以前的表設計,頗有怨言。這樣的一個sql,增加了複雜度,還沒寫注釋,讓人怎麼看啊。

 

Mysql的收穫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.