我有这个php脚本,每个p
只显示一个用户。account_id
,但不是那样的。
如果用户在同一帐户中有另外两个用户,则也会显示该用户。我无法解决任何问题。
$groupConfig = [
'Administrator' => ['groupId' => 5, 'accountType' => [5]],
'Community Manager' => ['groupId' => 4, 'accountType' => [4]],
'Gamemaster' => ['groupId' => 3, 'accountType' => [4]],
'Tutor' => ['groupId' => 1, 'accountType' => [2, 3]],
];
foreach ($groupConfig as $groupTitle => $group) {
$k = $SQL->query('SELECT
`p`.`name`,
`p`.`lastlogin`,
`p`.`id`,
`p`.`group_id`,
`a`.`type`
FROM
`players` AS `p`
LEFT JOIN `accounts` AS `a`
ON `a`.`id` = `p`.`account_id`
WHERE `a`.`type`
IN (' . implode(',', $group['accountType']) . ')
AND group_id = ' . $group['groupId'] . '
ORDER BY
`group_id`
DESC')->fetchAll();
}
答案 0 :(得分:0)
您可以尝试使用GROUP BY
子句按相同的值(p.account_id
)进行分组。
为什么您的数据允许每个帐户有多个用户?
$groupConfig = [
'Administrator' => ['groupId' => 5, 'accountType' => [5]],
'Community Manager' => ['groupId' => 4, 'accountType' => [4]],
'Gamemaster' => ['groupId' => 3, 'accountType' => [4]],
'Tutor' => ['groupId' => 1, 'accountType' => [2, 3]],
];
foreach ($groupConfig as $groupTitle => $group) {
$k = $SQL->query('SELECT
`p`.`name`,
`p`.`lastlogin`,
`p`.`id`,
`p`.`group_id`,
`a`.`type`
FROM
`players` AS `p`
LEFT JOIN `accounts` AS `a`
ON `a`.`id` = `p`.`account_id`
WHERE `a`.`type`
IN (' . implode(',', $group['accountType']) . ')
AND group_id = ' . $group['groupId'] . '
GROUP BY `p`.`account_id`
ORDER BY
`group_id`
DESC')->fetchAll();
}
答案 1 :(得分:0)
将fetchAll更改为fetch,或添加" LIMIT"到SQL查询。你得到多个结果是绝对正常的。您不是要从表帐户查询,而是查询表格玩家。因此,您的结果将包含与帐户有玩家一样多的行。最好的方法是查询正确的表,但您也可以使用前面提到的修复程序解决它。