Mysql左外部联接,右表中的列不是外键
我对左外连接的理解是Mysql左外部联接,右表中的列不是外键,mysql,sql,left-join,Mysql,Sql,Left Join,我对左外连接的理解是 table1: id(pk) name(unique_key) address phone table2: new_id(pk) name(foreign key) company work-experience 1st table: 1 a x 123 2 b y 234 3 c z 345 2nd table 1 a aaa 2 2 a aab 3 3 b aab 3 如果我愿意的话 select * from table1 left outer joi
table1:
id(pk) name(unique_key) address phone
table2:
new_id(pk) name(foreign key) company work-experience
1st table:
1 a x 123
2 b y 234
3 c z 345
2nd table
1 a aaa 2
2 a aab 3
3 b aab 3
如果我愿意的话
select * from table1 left outer join table2 on table1.name=table2.name,
it will give me
1 a x 123 1 a aaa 2
1 a x 123 2 a aab 3
2 b y 345 3 b aab 3
3 c z 345 NULL NULL NULL NULL
现在,我想得到company为aab的所有行,而不是上面的结果。另外,对于第一个表中的任何条目,若第二个表中并没有对应的条目,那个么第二个表中的所有列都应该为NULL
像这样:
1 a x 123 aab 3
2 b y 234 aab 3
3 c z 345 NULL NULL
上述结果是否可能与左外连接有关?如果没有,如何获得上述结果?您只需在
左连接的部分的中添加第二个表(右侧表)的条件即可
SELECT * FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t1.name = t2.name AND
t2.company = 'aab'
此外,在多表查询的情况下,为了代码清晰(增强可读性),并避免明确的行为,建议使用。从表1中选择t1.name、t1.address、t1.phoneNo、t2.comanay、t2.workExperiance作为t1
左外联接表2为t1上的t2。名称=t2。名称和
t2.company='aab'