简体   繁体   English

Mysql 一对多关系

[英]Mysql one to many relationship

I have two tables:我有两张桌子:

TABLE items with IDItem field TABLE comments with IDcomment, IDItem, datePublished and comment fields.带有 IDItem 字段的 TABLE 项目 带有 IDcomment、IDItem、datePublished 和评论字段的 TABLE 评论。

If I want to list last 10 comments it is no problem, I just sort the 'comments' table.如果我想列出最后 10 条评论没问题,我只需对“评论”表进行排序。 The problem is when I want to list last ten comments on the individual items, that means items are not duplicated.问题是当我想列出单个项目的最后十条评论时,这意味着项目不会重复。

Any best way to achieve this in regards to using indexes?在使用索引方面有什么最好的方法来实现这一点? If I order by 'comments' and group by IDItem I don't get the last comment out on each item as the group seems to order randomly:-(如果我按“评论”排序并按 IDItem 分组,我不会得到每个项目的最后一条评论,因为该组似乎是随机排序的:-(

I found solution to bring 'lastDate' to the 'items' table, so I can sort by the items and I will have the correct sort order, but when I join to the comments table I get 10 rows of the same item id if it had 10 comments:-(我找到了将“lastDate”带到“items”表的解决方案,这样我就可以按项目排序,并且我会有正确的排序顺序,但是当我加入评论表时,如果它,我会得到 10 行相同的项目 id有 10 条评论:-(

How is the proper way to join one to many so I get only one item from the left table with one item on the right table?如何正确连接一对多,以便我从左表中只获得一项,而在右表中获得一项?

I am not sure if I was very clear.我不确定我是否很清楚。

It sounds like you're trying to get the 10 items returned that have the most recent 10 comments, with one comment per item correct?听起来您正在尝试返回具有最近 10 条评论的 10 条项目,每个项目有一条评论是否正确?

If so, try this:如果是这样,试试这个:

SELECT * FROM Items I
JOIN
(SELECT TOP 10 * FROM Comments C2 WHERE DatePublished=
       (SELECT MAX(DatePublished) FROM Comments C3 WHERE C2.IDItem=C3.IDItem)
       ORDER BY DatePublished DESC) C1
ON I.IDItem=C1.IDItem

Edited: Removed extra SELECT and added limit of 10 comments returned编辑:删除了额外的 SELECT 并添加了返回 10 条评论的限制

DECLARE @item TABLE
(
  IDItem int
)
DECLARE @comment TABLE
(
  IDComment int,
  DatePublished date,
  IDItem int,
  Comment varchar(100)
)

INSERT INTO @item (IDItem) VALUES (1);
INSERT INTO @item (IDItem) VALUES (2);
INSERT INTO @item (IDItem) VALUES (3);
INSERT INTO @item (IDItem) VALUES (4);

INSERT INTO @comment (IDComment, DatePublished, IDItem, Comment) VALUES (1,'2011-01-01', 1, 'test1');
INSERT INTO @comment (IDComment, DatePublished, IDItem, Comment) VALUES (2,'2011-01-02', 1, 'test2');
INSERT INTO @comment (IDComment, DatePublished, IDItem, Comment) VALUES (3,'2011-01-01', 2, 'test3');
INSERT INTO @comment (IDComment, DatePublished, IDItem, Comment) VALUES (4,'2011-01-03', 2, 'test4');
INSERT INTO @comment (IDComment, DatePublished, IDItem, Comment) VALUES (5,'2011-01-02', 3, 'test5');
INSERT INTO @comment (IDComment, DatePublished, IDItem, Comment) VALUES (6,'2011-01-05', 3, 'test6');

SELECT i.IDItem, (SELECT TOP 1 c.Comment FROM @comment c WHERE c.IDItem = i.IDItem ORDER BY c.DatePublished) FROM @item i

RETURNS回报

   1    test1
   2    test3
   3    test5
   4    NULL

If it's what are you looking for, just mysql this code.如果您正在寻找它,只需 mysql 这个代码。 Replace TOP 1 with LIMIT 1 etc.将 TOP 1 替换为 LIMIT 1 等。

(Updated) What about this? (更新)这个呢?

SELECT IDcomment, IDitem from COMMENTS where IDitem in (SELECT DISTINCT(IDitem) FROM comments); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM