如何从没有匹配列的表中获取数据?

时间:2017-03-15 23:26:04

标签: php mysql

我一直在试图找到一个完美的查询,让我能够显示参与特定项目对话的所有人的名字和姓氏。

对话表

convo_id | project_id | toEmployee_id | fromEmployee_id | message

员工

employee_id | first_name | last_name |

myinbox.php

SELECT * FROM projects as p
JOIN employeeprojects AS ep
ON p.project_id = ep.project_id
JOIN employees AS e
ON ep.assigned_by = e.employee_id
JOIN clients AS c
ON p.client_id = c.id
WHERE ep.employee_id='$session_myemployeeid'

<a data-toggle="tooltip" title="view conversation"  href='conversation_feed.php?viewproject=conversation&emprojectid=<?=$employeeproject['project_id'];?>View Conversation</a>

displayconversation.php

$projectconvoid =  $_GET['emprojectid'] ;
SELECT * FROM employeeprojects_conversation AS epc
JOIN projects AS p
ON epc.project_id=p.project_id
JOIN employeeprojects AS ep
ON p.project_id=ep.project_id
WHERE epc.project_id='$projectconvoid'

虽然这一切都非常适合与相关人员展示特定的项目对话,但我希望能够显示他们的名字,而不是employee_id

我该怎么做?

1 个答案:

答案 0 :(得分:1)

加入employees表两次,一次用于toEmployee_id,另一次用于fromEmployee_id,如下所示:

SELECT
    *
FROM
    employeeprojects_conversation AS epc
JOIN projects AS p
    ON epc.project_id=p.project_id
JOIN employeeprojects AS ep
    ON p.project_id=ep.project_id
JOIN employees AS emp
    ON emp.employee_id = epc.toEmployee_id
JOIN employees AS emp2
    ON emp2.employee_id = epc.fromEmployee_id
WHERE
    epc.project_id='$projectconvoid'