Three tables LEFT JOIN query SQL statement Writing _ database other
Source: Internet
Author: User
Today I want to check all the Realname Username,psw,gname,tel
Table structure:
Table T1
Field Name: T1_ID,USERNAME,PSW
Table T2
Field Name: t2_id,gname,t1_id//Here a 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 when a realname in the T2 table does not have content, but also to the Username,psw,tel of this realname display, direct use
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: T2 table must have a realname work, in order to query out his information, this is certainly not the desired result, finally thought of using the left connection, the SQL statement is:
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"
This will result in the desired results.
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