0

好吧,我无法在标题中具体说明我的问题,所以请原谅我。

我制作了一个 .php 脚本,它从我的数据库中读取数据并将其打印到页面上的表格中。基本上它应该作为banlist。但我遇到了一个问题。由于禁令在数据库中的几个不同表中排序,因此我需要阅读所有这些表以了解我在禁令列表中需要的具体细节。我完成了大部分工作,但我无法获得禁止“骗子”的管理员的姓名。这是“admin_id”如何位于“penalties”表中而管理员的名称位于“clients”表中的方式。现在我不知道如何通过“penalties”表中的“admin_id”从“clients”表中获取管理员的名称并在同一页面上打印。

所以就是我所做的,只是缺少“管理员”名称。

这是从数据库中读取当前信息的代码。

mysql_query("SELECT penalties.id, penalties.type, penalties.time_add, penalties.time_expire,
                    penalties.reason, penalties.inactive, penalties.duration, penalties.admin_id,
                    target.id as target_id, target.name as target_name, target.ip as target_ip 
             FROM penalties, clients as target 
             WHERE (penalties.type = 'TempBan' OR penalties.type = 'Ban')
                  AND inactive = 0 
                  AND penalties.client_id = target.id
             ORDER BY penalties.id DESC") 
  or die(mysql_error());
4

2 回答 2

1

这应该为您指明正确的方向:

SELECT 
penalties.id, penalties.type, penalties.time_add, penalties.time_expire, penalties.reason, penalties.inactive, penalties.duration, penalties.admin_id, 
clients.id as target_id, clients.name as target_name, clients.ip as target_ip 
FROM penalties 
LEFT JOIN clients
ON penalties.client_id = clients.id 
WHERE (penalties.type = 'TempBan' OR penalties.type = 'Ban')  AND inactive = 0
ORDER BY penalties.id DESC
于 2013-07-02T17:44:44.600 回答
0

您有两个从惩罚返回到客户端的逻辑连接。因此,您需要返回两个连接。一个用于“目标”,一个用于“管理员”

SELECT penalties.id, penalties.type, penalties.time_add, penalties.time_expire, penalties.reason, 
       penalties.inactive, penalties.duration, penalties.admin_id, target.id as target_id, 
       target.name as target_name, target.ip as target_ip,
       admin.name
FROM penalties, clients as target, clients as admin

WHERE (penalties.type = 'TempBan' OR penalties.type = 'Ban')  
  AND inactive = 0 
  AND penalties.client_id = target.id 
  AND penalties.admin_ID = admin.id

ORDER BY penalties.id DESC
于 2013-07-02T17:46:13.283 回答