MySQL SQL語句之join過濾注意事項

來源:互聯網
上載者:User

對於SQL語句的JOIN語句,大家並不陌生。關於其中的where語句中進行內容過濾還是在 On 語句中或者having進行過濾,這個是值得注意的。

案例:找出某段時間內註冊的新使用者,沒有通過新手任務當中具體哪一步任務,統計

其中涉及到兩張表

使用者基礎資料表:user_base,  user_id, 註冊時間 created_at

任務表: user_id,task_id,task_status

A 語句:

select  min(b.task_id) life,task_status,  a.user_id from user_tasks  b  join  (select user_id from  user_bases where created_at  between '   '  and  '   ' ) a  on a.user_id=b.user_id group by a.user_id having  b.task_status<3;

B語句:

select min(task_id),task_status,a.user_id from user_tasks b join  (select user_id from  user_bases where created_at between '  '  and  '  '  ) a  on a.user_id=b.user_id  and task_status<3 group by user_id;

A,B語句最大的差別是對 task_status<3 進行過濾的先後順序。

A,語句中表內容進行連結匹配時,以user_id為準,找出每個使用者的task_id,最後是 having  task_status<3 過濾

B,語句是表內容進行匹配時,task_status<3進行join並且按照group by  求出每個使用者min  task_id.

所以B語句是符合標準的。

對於Join 進行連結時,要確定好以哪些欄位進行過濾內容!

本文出自 “技術成就夢想” 部落格,請務必保留此出處http://weipengfei.blog.51cto.com/1511707/1117069

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/database/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.