使用JOIN链接表

时间:2014-01-17 16:14:34

标签: php mysql

我有3张桌子; memberID,会员,人

我需要来自'membership'的名字姓氏 来自'memberID'的MemberID和PersonID 以下给出了一个名单。

   SELECT 
   CONCAT(P.FirstName, ' ', P.LastName) as FullName
   FROM `membership` M
   INNER JOIN `memberID` MI ON M.`MembershipID` = MI.`MembershipID`
   INNER JOIN `person` P ON MI.`PersonID` = P.`PersonID`
   AND P.`IsActive` =1

以下失败,如何添加PersonID和MembershipID列,。 我相信我需要另一个INNER JOIN,但我不知道如何构建它。

   SELECT
   CONCAT(P.FirstName, ' ', P.LastName) as FullName
   FROM `membership` M
   MI.'membershipID' MI.'PersonID' FROM  'membershipID' MI
   INNER JOIN `memberID` MI ON M.`MembershipID` = MI.`MembershipID`

   INNER JOIN `person` P ON MI.`PersonID` = P.`PersonID`
   AND P.`IsActive` =1

1 个答案:

答案 0 :(得分:1)

可能这些列已经在被引用的表中:

SELECT CONCAT(P.FirstName, ' ', P.LastName) as FullName,
       MI.PersonID, M.`MembershipID`
FROM `membership` M INNER JOIN
     `memberID` MI
     ON M.`MembershipID` = MI.`MembershipID` INNER JOIN
     `person` P
     ON MI.`PersonID` = P.`PersonID` AND P.`IsActive` = 1;

您不需要其他联接,只需要select中的其他列。