MySQL 结果 - “分组依据”删除不正确的重复项
我会尽力描述我遇到的问题:)
我论坛中的每个线程/主题都代表一张光盘。论坛的注册会员使用一系列复选框(每张光盘旁边显示一个)来勾选他们收藏的每张光盘。当表单被 $_POST'ed 时,它将信息存储在表中,如下所示
:用户 ID - 光盘 ID |
+--------------------------------+
| 2 - 571 |
| 2 - 603 |
| 2 - 4532 |
当用户下次查看论坛时,我会在用户拥有的光盘上勾选并禁用复选框。这是使用以下方法完成的:
$sql = 'SELECT id, poster, subject, posted, last_post, last_post_id,
last_poster, num_views, num_replies, closed, sticky, moved_to, topicimage,
c.user_id, c.disc_id FROM topics LEFT JOIN collections AS c ON c.disc_id=id
WHERE forum_id='.$id.' ORDER BY sticky DESC;
上面抓取了所有光盘,然后我使用以下(精简的)代码显示它们:
$result = $db->query($sql) or error('Unable to fetch topic list '.$sql.'', __FILE__, __LINE__, $db->error());
// If there are topics in this forum
if ($db->num_rows($result))
{
while ($cur_topic = $db->fetch_assoc($result)) { // 如果登录用户 ID 与当前光盘 user_id 匹配(即如果该用户拥有该光盘) if ($cur_topic['user_id']==$pun_user['id']) { $read = '
我拥有这个!'; } 别的 { $read = '
我拥有这个!'; } } 这
非常有效,直到第二个用户将相同的光盘 ID 添加到他的收藏中,例如:
|用户 ID - 光盘 ID |
+--------------------------------+
| 2 - 571 |
| 2 - 603 |
| 6 - 571 | 6 - 571
这会导致论坛中出现重复的主题。一个被正确勾选(因为我拥有它),另一个未被勾选,尽管它共享所有相同的信息,例如主题 ID 和图像。
我的第一个想法是尝试将 GROUP BY c.disc_id 添加到 SQL,这确实成功删除了重复的主题 - 但是,它删除了错误的主题。我勾选的光盘不再显示,只留下未勾选的版本。
希望这是有道理的。任何人都可以提供任何见解或想法吗? 非常感谢。
Will do my best to describe the problem Im having :)
Each thread/topic in my forum represents one disc. Registered members of the forum use a series of checkboxes (one displayed next to each disc) to tick each disc that they have in their collection. When the form is $_POST'ed it stores the information in a table like so:
| user_id - disc_id |
+--------------------+
| 2 - 571 |
| 2 - 603 |
| 2 - 4532 |
When the user next views the forum I have the checkboxes ticked and disabled on discs that the user owns. This is done using:
$sql = 'SELECT id, poster, subject, posted, last_post, last_post_id,
last_poster, num_views, num_replies, closed, sticky, moved_to, topicimage,
c.user_id, c.disc_id FROM topics LEFT JOIN collections AS c ON c.disc_id=id
WHERE forum_id='.$id.' ORDER BY sticky DESC;
The above grabs all of the discs, which I then display using the following (stripped down) code:
$result = $db->query($sql) or error('Unable to fetch topic list '.$sql.'', __FILE__, __LINE__, $db->error());
// If there are topics in this forum
if ($db->num_rows($result))
{
while ($cur_topic = $db->fetch_assoc($result))
{
// If logged in users ID matches the current discs user_id (i.e if this user owns this disc)
if ($cur_topic['user_id']==$pun_user['id']) {
$read = '
I own this!';
} else {
$read = '
I own this!';
}
}
}
This works great, until a second user adds the same disc ID to his collection, eg:
| user_id - disc_id |
+--------------------+
| 2 - 571 |
| 2 - 603 |
| 6 - 571 |
This causes a duplicate thread to appear in the forum. One is correctly ticked (because I own it), the other is not ticked, though it shares all of the same information such as topic id and image.
My first thought was to try adding GROUP BY c.disc_id to the SQL, which does successfully remove the duplicate topic - However, it is removing the wrong one. The disc that I have ticked is no longer shown, leaving only the unticked version.
Hope that makes sense. Can anyone offer any insight or ideas?
Many Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个猜测,因为我不知道您的架构,但我没有看到您在 WHERE 子句中指定用户的 ID。
像下面这样的东西怎么样?
另外,您加入的主题 ID = 光盘 ID。这是故意的吗?
This is a guess, since I don't know your schema, but I don't see you specifying the user's ID in your WHERE clause.
What about something like the following?
Also, you're joining on Topic ID = Disc ID. Is that intentional?
我可以看到两种简单的方法来解决这个问题:
第一个:
使用两个查询,对查询进行分组,第二个来获取用户拥有的所有disc_id,
第二个:
使用第一个查询:
I can see two easy way for solving this:
first:
with two query, you group query and a second to fetch all the disc_id owned by the user
second:
with your first query: