gpt4 book ai didi

php - SQL获取多个相同行

转载 作者:行者123 更新时间:2023-11-29 07:28:34 24 4
gpt4 key购买 nike

<强> $uid is 6

    public function getUserGroups($uid) {
$sql = "SELECT groups.id, groups.name, groups.type , groups.img_path, DATE_FORMAT(user_group.joined_at,'%e/%c/%Y %H:%i') joined_at FROM groups JOIN user_group ON groups.id = user_group.group_id WHERE groups.id IN (SELECT group_id FROM user_group WHERE user_id = ?)";
$query = $this->db->prepare($sql);
$query->execute(array($uid));
if($query->rowCount()) {
$user_groups = $query->fetchAll();
return $user_groups;
}
return false;
}

此函数多次从表中获取真实数据。这里的输出:

array (size=2)
0 =>
array (size=10)
'id' => string '4' (length=1)
0 => string '4' (length=1)
'name' => string 'Test Grubu' (length=10)
1 => string 'Test Grubu' (length=10)
'type' => string 'Açık Grup' (length=11)
2 => string 'Açık Grup' (length=11)
'img_path' => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
3 => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
'joined_at' => string '9/11/2015 00:48' (length=15)
4 => string '9/11/2015 00:48' (length=15)
1 =>
array (size=10)
'id' => string '4' (length=1)
0 => string '4' (length=1)
'name' => string 'Test Grubu' (length=10)
1 => string 'Test Grubu' (length=10)
'type' => string 'Açık Grup' (length=11)
2 => string 'Açık Grup' (length=11)
'img_path' => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
3 => string 'images/groupimages/11-09-2015-12-48-56_ic_participation_off.png' (length=63)
'joined_at' => string '9/11/2015 11:50' (length=15)
4 => string '9/11/2015 11:50' (length=15)

用户组:

Group

群组:

enter image description here

我想获取用户的所有组。我保存到 user_group,所有组都保留在组中。

最佳答案

如果我真正理解你的问题,我想是这样的:

SELECT
groups.id,
groups.name,
groups.type,
groups.img_path,
DATE_FORMAT(user_group.joined_at, '%e/%c/%Y %H:%i') joined_at
FROM groups
JOIN user_group ON groups.id = user_group.group_id
WHERE groups.id IN (SELECT group_id
FROM user_group
WHERE user_id = ?)
GROUP BY groups.id;

关于php - SQL获取多个相同行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33606805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com