如何在条件链接到第三个表的情况下联接两个表

时间:2018-12-19 22:53:02

标签: mysql

我有3张桌子

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

我想让连接表1和表2的条件保持链接到第三张表。

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)

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

1 个答案:

答案 0 :(得分:3)

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