连接两个表,连续两个用户名称信息

时间:2018-04-19 13:56:57

标签: php mysql

我有一个table_resident,如下图所示

enter image description here

然后我有一个table_complaints,如下面的图片

enter image description here

使用table_resident.resident_id = table_complaints.resident_id以及table_resident.resident_id = table_complaints.party_id

如何将resident_id = 1和party_id = 2名称加入同一行,如下图所示?我可以看到resident_id = 1 first_name,last_name但是在resident_id = 2中如何使用party_id再次看到它们?

enter image description here

这是我加入的代码但在party_id中我不知道我将再次使用名字,姓氏,中间名

SELECT
  table_involvement.*,
  table_resident.first_name,
  table_resident.last_name, 
  table_resident.middle_name,
  table_complaints.*
FROM ((
  table_complaints
    LEFT JOIN table_resident
      ON table_complaints.resident_id = table_resident.resident_id)
    LEFT JOIN table_involvement
      ON table_complaints.complaints_id = table_involvement.complaints_id)
ORDER BY table_resident.first_name ASC";

1 个答案:

答案 0 :(得分:0)

这应该这样做。我用户2内连接来实现这个解决方案。

SELECT TABLE_RESIDENT.FIRST_NAME, TABLE_RESIDENT.LAST_NAME, 
TABLE_RESIDENT.MIDDLE_NAME, TABLE_COMPLAINTS.*, TABLE_INVOLVEMENT.* 
FROM TABLE_RESIDENT
INNER JOIN TABLE_COMPLAINTS 
ON TABLE_COMPLAINTS.RESIDENT_ID = TABLE_RESIDENT.RESIDENT_ID
INNER JOIN TABLE_INVOLVEMENT
ON TABLE_COMPLAINTS.RESIDENT_ID = TABLE_INVOLVEMENT.COMPLAINTS_ID
WHERE TABLE_COMPLAINTS.PARTY_ID = 2
ORDER BY TABLE_RESIDENT.FIRST_NAME ASC