简体   繁体   English

在多行同一表中选择联接

[英]SELECT JOIN in same table ON multiple rows

I have a table with an itemID and a categoryID columns. 我有一个带有itemID和categoryID列的表。 Both columns are primary keys because each item can have more than 1 category: 两列都是主键,因为每个项目可以具有多个类别:

itemID  |  catID
-----------------
1        |  2
1        |  3
1        |  4
2        |  2
2        |  3
2        |  4

I want to select items with the same categories (based on all the item categories, not just one) so I need to kind of JOIN the same table. 我想选择具有相同类别的项目(基于所有项目类别,而不仅仅是一个类别),因此我需要加入同一张表。

I want to be able to find itemIDs with the same catIDs based on a specified itemID. 我希望能够基于指定的itemID查找具有相同catID的itemID。

Note: For example item 1 have categories 2,3,4,5,6 and item 2 have categories 2,3,4,5,6 and item 3 have categories 3,5,6 then if i compare item 1 to item 2 and 3 i need to get item 2 first and then item 3 because item 2 have more categories matches than item 3.. Obviously it need to be done with all the items not only 3.. This way I can recommend visitors of similar products... 注意:例如,项目1具有类别2,3,4,5,6,项目2具有类别2,3,4,5,6,项目3具有类别3,5,6,那么如果我将项目1与项目2进行比较和3我需要先获得第2项,然后第3项,因为第2项比第3项具有更多的类别匹配项。显然,不仅所有3.项都需要完成所有项的匹配。这样,我可以推荐类似产品的访客。 ..

So you want to choose one itemID, then match it to all other itemID's that share one or more catID's? 因此,您想选择一个itemID,然后将其与共享一个或多个catID的所有其他itemID相匹配?

SELECT DISTINCT c2.itemID
FROM categories c1
JOIN categories c2 ON c1.catID = c2.catID
WHERE c1.itemID = ?

Building on Bill's initial query, this ought to order it in descending order by the number of categories matched (since the join should return one row per match). 以Bill的初始查询为基础,此查询应按匹配类别的数量以降序排列(因为联接每次匹配应返回一行)。 I also excluded the item being queried on from the result. 我还从结果中排除了要查询的项目。

SELECT c2.itemID
FROM categories c1
JOIN categories c2 ON c1.catID = c2.catID
WHERE c1.itemID = :id
AND c2.itemID <> :id
GROUP BY c2.itemID
ORDER BY count(c2.itemID) DESC;

You have many to many relation, so the query will look like: 您具有多对多关系,因此查询将如下所示:

SELECT item.name 
  FROM item AS b 
   JOIN itemcategories AS ab ON b.ID = ab.itemID 
 where ab.catID =2;

I want to select items with the same categories (based on all the item categories, not just one) so I need to kind of JOIN the same table. 我想选择具有相同类别的项目(基于所有项目类别,而不仅仅是一个类别),因此我需要加入同一张表。

Without mentioning anything else about the output, you can do something like: 在不提及输出的任何其他内容的情况下,您可以执行以下操作:

Select C.itemid
From categories As C
Where Exists    (
                Select 1
                From categories As C2
                Where C2.catID = C.catID
                    And C2.itemID <> C.itemID
                )
   And C.itemID = ?
Group By C.itemid

(From comments) (摘自评论)

Something like that, but ORDERED by items that have the most categories matches. 诸如此类,但按类别匹配最多的项目排序。 For example item 1 have categories 2,3,4,5,6 and item 2 have categories 2,3,4,5,6 and item 3 have categories 3,5,6 then if i compare item 1 to item 2 and 3 i need to get item 2 first and then item 3 because item 2 have more categories matches than item 3 例如,项目1具有类别2、3、4、5、6,项目2具有类别2、3、4、5、6,项目3具有类别3、5、6,则如果我将项目1与项目2和3进行比较我需要先获得项目2,然后再获得项目3,因为项目2比项目3具有更多的类别匹配项

This adds a different complexion to the question and is why you should include expected output in your original post. 这给问题增加了另一种肤色,这就是为什么您应该在原始帖子中包括预期的输出。 Taking what you have written literally: 以您的字面意思写成:

Select C.itemid, Group_Concat(C.catID Order By C.catID ) As Categories
    , Count(*) As MatchCount
From categories As C
    Join categories As C2
        On C2.itemID <> C.itemID
            And C2.catID = C.catID
Group By C.itemID
Order By Count(*) Desc

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

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