A multi-Table query SQL statement

Source: Internet
Author: User

Table 1: Table 2:
Id info id value
1 a 1 10
2 B 2 30
3 c
4 d

Want to get
Id value
1 10 // Table 2 exists
2 20 // Table 2 exists
3 0 // Table 2 does not exist
4 0 // Table 2 does not exist
Whether an SQL statement can be used to solve the problem, instead of obtaining the id from table 1 first, and then finding whether the record exists in Table 2. If yes, it is read directly, if not, the value is 0.

Copy codeThe Code is as follows:
Select Table 1.id, isnull (value, 0) from table 1
Left join table 2
On Table 1.id= table 2.id
 



 Copy codeThe Code is as follows:
Select a. id, isnull (B. value, 0) from table 1
Left join Table 2 B
On a. id = B. id

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.