0

我有以下 SQL:

$queryString = "
            SELECT     
                iR.lastModified,
                d.*,
                c2.title as stakeholderTitle,
                u.username as authorUsername,
                c.title as authorContactName,  
                GROUP_CONCAT(iR.stakeholderRef) AS participants
            FROM
                informationRelationships iR,
                contacts c2
            INNER JOIN
                debriefs d ON
                d.id = iR.linkId 
            LEFT JOIN
                users u ON
                u.id = iR.author      
            LEFT JOIN
                contacts c ON
                c.ref = u.contactId
            LEFT JOIN 
                debriefs d2 ON
                d2.stakeholder = c2.ref                
            WHERE               
                ( 
                    iR.clientRef = '$clientRef' OR  
                    iR.contactRef = '$contactRef'   
                )
                AND    
                    iR.projectRef = '$projectRef' AND
                    iR.type = 'Debrief' 
            GROUP BY
                iR.linkId
            ORDER BY
                d.dateOfEngagement                    
        ";     

请注意我需要 2 位不同的数据用于联系人表。

所以在某一时刻,我需要匹配

c.ref = u.contactId 

这将返回一位信息

但我还需要一个完全不同的分组:

d2.stakeholder = c2.ref

问题是标题是我对两者都感兴趣的列:

c2.title as stakeholderTitle,
...
c.title as authorContactName

我该怎么做呢?

我目前的尝试正在返回:

Error: Unknown column 'iR.linkId' in 'on clause'

在此处输入图像描述

我不确定我是否真的了解这里发生的事情:

如何在 mysql 和 php 中加入两个关于公共属性的表?

编辑::::---回答--zerkms

    $queryString = "
        SELECT     
            iR.lastModified,
            d.*,
            c2.title as stakeholderTitle,
            u.username as authorUsername,
            c.title as authorContactName,  
            GROUP_CONCAT(iR.stakeholderRef) AS participants
        FROM
            informationRelationships iR            
        INNER JOIN
            debriefs d ON
            d.id = iR.linkId  
        INNER JOIN 
            contacts c2 ON
            d.stakeholder = c2.ref  
        LEFT JOIN
            users u ON
            u.id = iR.author      
        LEFT JOIN
            contacts c ON
            c.ref = u.contactId             
        WHERE               
            ( 
                iR.clientRef = '$clientRef' OR  
                iR.contactRef = '$contactRef'   
            )
            AND    
                iR.projectRef = '$projectRef' AND
                iR.type = 'Debrief' 
        GROUP BY
            iR.linkId
        ORDER BY
            d.dateOfEngagement                    
    ";     

通过重新排序我的查询,我设法获得了两列......谢谢zerkms!

4

2 回答 2

0

您不能在 mysql 的单个查询中混合隐式连接和显式连接。

所以

FROM informationRelationships iR,
     contacts c2

应该改写为

FROM informationRelationships iR
INNER JOIN contacts c2 ON ...
于 2013-06-24T10:45:33.860 回答
0

不要在同一个查询(不是子查询)中使用笛卡尔积和联接,这里只使用联接(CROSS JOIN 与笛卡尔积相同)。

于 2013-06-24T10:49:08.323 回答