从mySQL数据库读取多个表


Read multiple tables from mySQL database?

我不能在标题中具体说明我的问题,所以请原谅我。

我制作了一个.php脚本,从数据库中读取数据,并将其打印到页面上的表中。基本上,它应该起到公告牌的作用。但我有一个问题。由于禁令在数据库中的几个不同的表中进行了排序,我需要阅读所有的禁令,以了解我在禁令列表中需要的具体细节。我已经完成了大部分,但我不知道谁禁止"骗子"管理员的名字。事情是这样的:"admin_id"位于"penalties"表中,admin的名称位于"clients"表中。现在我不知道如何从"clients"表中通过"penalties"表中的"admin_id"获得管理员的名称,并将其打印在同一页上。

所以这就是我所做的,只是缺少"管理员"的名字。

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

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());

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

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

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

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