mysql查詢語句中使用者變數的使用

來源:互聯網
上載者:User

標籤:

先上代碼吧

SELECT `notice`.`id` , `notice`.`fid` , `notice`.`has_read` , `notice`.`notice_time` , `notice`.`read_time` , `f`.`fnum` , `f`.`forg` , `f`.`fdst` , `f`.`actual_parking` AS `parking` , `f`.`scheduled_deptime` , `f`.`estimated_deptime` , `f`.`actual_deptime` , `f`.`scheduled_arrtime` , `f`.`estimated_arrtime` , `f`.`actual_arrtime` , `f`.`is_vip` , `f`.`aoc_notice`FROM (select t.id, t.fid, t.has_read, t.notice_time, t.read_timefrom(select a.id, a.fid, a.has_read,a.notice_time, a.read_time,@v_rownum := @v_rownum+1,if(@v_fid=a.fid,@v_rowid:[email protected]_rowid+1,@v_rowid:=1 ) as row_count,@v_fid:=a.fidfrom(SELECT id, fid, has_read, notice_time, read_time FROM vkm_user_notice_flightwhere  `notice_type` = ‘process_update‘ and uid=82order by fid, notice_time desc) a,(   select @v_rownum:=0, @v_rowid:=0,  @v_fid:=null) b) twhere t.row_count =1) AS `notice`LEFT JOIN `vkm_flight` AS `f` ON `notice`.`fid` = `f`.`id`LEFT JOIN `vkm_parking` AS `parking` ON `f`.`actual_parking` = `parking`.`parking_num`

 在工作中朋友發來了一段這樣的sql語句 一開始我 就蒙了 根本不懂啊!因為個人的mysql也不是很精通只會簡單的增刪改而已其實,以上代碼很直接的寫法就是

SELECT `notice`.`id` , `notice`.`fid` , `notice`.`has_read` , `notice`.`notice_time` , `notice`.`read_time` , `f`.`fnum` , `f`.`forg` , `f`.`fdst` , `f`.`actual_parking` AS `parking` , `f`.`scheduled_deptime` , `f`.`estimated_deptime` , `f`.`actual_deptime` , `f`.`scheduled_arrtime` , `f`.`estimated_arrtime` , `f`.`actual_arrtime` , `f`.`is_vip` , `f`.`aoc_notice`FROM `notice` LEFT JOIN `vkm_flight` AS `f` ON `notice`.`fid` = `f`.`id` LEFT JOIN `vkm_parking` AS `parking` ON `f`.`actual_parking` = `parking`.`parking_num`

 但是以上代碼的查詢效率真的是差別太大了!具體的上面的那串代碼我還沒明白方式,也請求指導中,但是上網百度了一下 一個是mysql中if的使用還有一個就是mysql中使用者變數的使用

使用者名稱量的設定可以通過set var value的方式也可以用以上的形式@var:=val;

mysql中if的使用if(exp1,exp2,exp3) 在if中如果exp1為true則執行exp2否則執行exp3;

現在再看上述的代碼的話可能就簡單多了!根據上述的代碼我又重新自己寫了一個簡單的應用執行個體

select id,fnum,forg,fdst,@v_rownum:[email protected]_rownum+1 from vkm_flight,(select @v_rownum:=0) b

 這樣就返回了@v_rownum的值

 

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.