我有以下SQL 2個不同的參數:的MySQL/PHP - 1個表
$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'
我不知道我真正明白髮生了什麼這裏:
how to join two tables on common attributes in mysql and php?
編輯:::: --- ANSWERED - 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!
它清楚地表明informationRelationships不包含名爲linkId的列。你能描述桌子嗎? – chetan