我有一个简单的JOIN查询:
$lstCheck = $dbWHMCS->query('SELECT * FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['ho.id'];
$clientid = $data['cl.id'];
}
我的问题是我在tblhosting和tblclients表中都有一个“id”列,所以我的变量都有相同的id。我尝试在上面的示例中使用别名设置它(ho。和cl。)但它不起作用。我怎么能在上面的例子中这样做?
谢谢!
答案 0 :(得分:1)
这个怎么样? (在php细节上有点生疏,但这应该这样做):
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}
答案 1 :(得分:1)
您正在使用通配符*
选择记录,因此您无法选择这样的字段。
根据您的查询h.hst_serverid
& ho.userid
具有您想要的完全相同的值。所以只需这样做
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hst_serverid'];
$clientid = $data['userid'];
}
但是,选择特定的行也可能听起来更好
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid, ....');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}