简体   繁体   English

从另一个表联接结果

[英]Joining Results From Another Table

I'm dealing with a large query that maps data from one table into a CSV file, so it essentially looks like a basic select query-- 我正在处理一个大型查询,该查询将一个表中的数据映射到CSV文件中,因此它基本上看起来像一个基本的选择查询-

SELECT * FROM item_table

--except that * is actually a hundred lines of CASE, IF, IFNULL, and other logic. -除了*实际上是一百行的CASE,IF,IFNULL和其他逻辑。

I've been told to add a "similar items" line to the select statement, which should be a string of comma-separated item numbers. 我被告知要在select语句中添加“相似项”行,该行应为一串用逗号分隔的项号。 The similar items are found in a category_table, which can join to item_table on two data points, column_a and column_b, with category_table.category_id having the data that identifies the similar items. 可以在category_table中找到相似的项目,该类别可以在column_a和column_b这两个数据点上与item_table联接,category_table.category_id具有标识相似项目的数据。

Additionally, I've been told NOT to use a subquery. 此外,有人告诉我不要使用子查询。

So I need to join category_table and group_concat item numbers from that table having the same category_id value (but not having the item number of whatever the current record would be). 因此,我需要从该表中连接具有相同category_id值的category_table和group_concat项目编号(但不具有与当前记录相同的项目编号)。

If I can only do it with a subquery regardless of the instructions, I will accept that, but I want to do it with a join and group_concat as instructed if possible--I just can't figure it out. 如果无论指令如何都只能使用子查询来执行此操作,我会接受的,但是如果可能的话,我希望使用join和group_concat进行操作-我无法弄清楚。 How can I do this? 我怎样才能做到这一点?

I think this is what you're looking for, although I wasn't sure what uniquely identified your item_table so I used column_a and column_b (those may be incorrect): 我认为这是您正在寻找的内容,尽管我不确定是什么唯一标识了您的item_table,所以我使用了column_a和column_b(可能不正确):

SELECT 
    ...,
    GROUP_CONCAT(c.category_id separator ',') CategoryIDs
FROM item_table i
   JOIN category_table ct ON i.column_a = ct.column_a AND
      i.column_b = ct.column_b
GROUP BY i.column_a, i.column_b

I've used a regular INNER JOIN , but if the category_table might not have any related records, you may need to use a LEFT JOIN instead to get your desired results. 我使用了常规的INNER JOIN ,但是如果category_table可能没有任何相关记录,则可能需要使用LEFT JOIN来获得所需的结果。

You can make use of a mySQL "feature" called hidden columns. 您可以使用称为隐藏列的mySQL“功能”。

I am going to assume you have an item id in the item table that uniquely identifies each row. 我将假设您在项目表中有一个项目ID,该ID可以唯一地标识每一行。 And, if I have your logic correct, the following query does what you want: 而且,如果我的逻辑正确,则以下查询将满足您的要求:

select i.*, group_concat(c.category_id)
from item_table i left outer join
     category_table c
     on i.column_a = c.column_a and
        i.column_b = c.column_b and
        i.item_id <> c.category_id
group by i.item_id

Maybe something like this? 也许是这样的吗?

SELECT i.*, GROUP_CONCAT(c.category_id) AS similar_items
  FROM item_table i
INNER JOIN category_table c ON (i.column_a = c.column_a AND
                                i.column_b = c.column_b)
GROUP BY i.column_a, i.column_b

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

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