如何使用链接到第三个表的条件连接两个表

k3bvogb1  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(255)

我有三张table

Table1 : ID, Description
Table2 : ID, FirstName, LastName
Table3 : FirstName, LastName, Flag

我想用一个链接到第三个表的条件左联接table1和table2。

Result Table: ID, Description, FullName (FirstName + LastName) 
(Condition: only the names that have flag set to true in table3 can be used in the resultant table)

如何完成以下任务?先谢谢你。

m528fe3b

m528fe3b1#

SELECT t1.ID, t1.Description, t2.FirstName + ' ' + t2.LastName as FullName
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
JOIN Table3 t3 ON t2.FirstName=t3.FirstName AND t2.LastName=t3.LastName
WHERE t3.Flag=1

相关问题