SQL statement for left join query in three tables

Source: Internet
Author: User

Query the username, psw, gname, and tel of all realnames today.
Table Structure:
Table t1
Field name: t1_id, username, psw
Table t2
Field name: t2_id, gname, t1_id // here one t1_id corresponds to multiple t2_id
T3
Field name: t3_id, realname, tel, t1_id // here, a t1_id corresponds to a t3_id
The trouble is that when a realname has no content in Table t2, the username, psw, and tel of this realname should be displayed and used directly.
SQL = "select username, psw, gname, tel from t1, t2, t3 where t1.t1 _ id = t2.t1 _ id and t1.t1 _ id = t3.t1 _ id"
The result is that the t2 table must have a realname file to query its information. This is definitely not the desired result. The SQL statement is as follows:
SQL = "select username, psw, gname, tel from (t1 left join t2 on t1.t1 _ id = t2.t1 _ id) left join t3 on t1.t1 _ id = t3.t1 _ id"
In this way, the desired result is obtained.

Related Article

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.