|
Groups | Jobs | User Space |
Hello, pls log in or register
|
|
How to Left Join It's about "LEFT JOIN" in SQL query. 1. What's Left Join? If you are seeking for an answer from internet for what’s going wrong with your SQL left join, you may try luck by directly go to item 3. If you would like learn something about left join, let’s start from the beginning. 1. What's Left Join? Secondly explain in programming: 2. How to implement? Now let’s back into the reality, as a DBA for an online store, you have 3 tables: Userid username table selling table visits Now, you boss comes to you, “give me the report of how many times the users visited our web”. With left join, a piece of cake: How does this happen? select users.username,visits.visitcount from users left join visits on users.userid = visits.userid
2. pull out the date from right table
3. match them together, keep the matched data from right table, screen out none-matched
Ok, now, your picky boss said, page views means nothing to me, show me the total money people spent on my web together with those who hasn’t. So you back and found in table selling there are multiple records for some users, how could left join work without 1 on 1 match? Try this out: Here’s what you get: username amount See? By doing the sub-query, { select some-date from a-left-table left join ( select another-data from another-table ) another-table on condition-leftside = condition-rightside }, which makes the left join quite powerful. To help understand the formula, separate it into several parts:
1. reported “Unknown column '' in 'on clause' error. Take a look at following syntax, which will report above error: SELECT users.username, selling.amount Here will report Unknown column 'selling.userid' in 'on clause'. Check this out: Will report syntax error. ( And also parentheses is required doing such kind of sub-query. SELECT users.username, selling.amount Syntax error, if you want to use “as”, use it thoroughly. error Column '' in on clause is ambiguous select users.username, selling.amount from users left join ( select userid,sum(amount) as amount from selling group by userid ) selling on users.userid = userid Very easy to understand, because you omit the users.userid = selling.userid It’s all about good habits on coding. How to get: How to get: Practise following commands: P.S. This is my first blog purly talking about technical staffs, and they are my personal opinions.
tags: @ 20080522 00:01:11 with 1 comments |
|
20080522 19:10:40:
freedo
from
funny thing is: from the title, "how to left join", someone thought I'm talking about HR again, talking about how to left a company and join again :D |
About Freedo 关于作者
微软中国 SQL Server 招聘顾问
研发中心:北京、上海
喜爱编程、轮滑
个人项目: 易麦
Microsoft Staffing Consultant
SQL Server China R&D Center
Living in Shanghai
Love Soccer; Roller & Coding;
和我联系 Contact:
Guestbook 给我留言
my facebook
my linkedin


Copyright 版权
本站博客全为原创。遵循
欢迎非商业用途转载,务必注明作者、原出处及超链接。
Recent comments:
== 20080804 16:42:57
已复 :)
== 20080801 14:06:53
这道题还真不太容易,我把 ...
== 20080604 22:50:38
Thanks for your report, which is tr ...
== 20080604 11:33:03
我一向用IE7或者Outlook的RSS ...
== 20080525 02:37:33
有意思。
== 20080522 19:10:40
funny thing is: from the title, &qu ...
Archieves
August 2008
July 2008
Jun 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
Jun 2007
Links 友情链接
易麦 e-mag
SQL team blog in Chinese
和我交换链接
加入 收藏
rss 订阅
|
|