简体   繁体   English

将多行合并为一个MySQL Join

[英]Combine multiple rows into one MySQL Join

I couldn't think of a better way to ask this question, but here goes: 我想不出更好的方式来问这个问题,但是这里有:

I have 2 tables. 我有2张桌子。 The first is a table of items for label printing jobs. 第一个是标签打印作业的项目表。 We'll call it pj_items The columns are simply: job_id, part_num and qty 我们称之为pj_items列只是: job_id, part_num and qty

The second table is a list of finishes called PartFinishes on parts with just 2 columns of: PartNumber and FinishId 第二个表是一个名为PartFinishes的完成列表,只有两列: PartNumber and FinishId

When I want to retrieve information for the label printer I use: 当我想检索标签打印机的信息时,我使用:
select pj_items.part_num, pj_items.qty, PartFinishes.FinishId from pj_items
join PartFinishes on PartFinishes.PartNumber = pj_items.part_num
where job_id = 1

This query works fine and gives me the right result when each part has only one finish. 此查询工作正常,并在每个部分只有一个完成时为我提供正确的结果。 The problem is when there are 2 or more finishes on a part. 问题是当零件上有两个或更多的表面处理时。

For example: The part 99401326 has 2 finishes. 例如:零件99401326有2个饰面。 On my pj_items table I have one row for that part so I can print one label. 在我的pj_items表上,我有一行用于该部分,因此我可以打印一个标签。 When I use this query it returns 2 rows for that part each with a different FinishId Unfortunately that would result in 2 labels printed. 当我使用这个查询时,它会为该部分返回2行,每行都有不同的FinishId不幸的是会导致打印2个标签。 I need both of those finishes on one row since I only want one label printed with both FinishId 's present. 我需要在一行上完成这两种饰面,因为我只想要一个印有两个FinishId的标签。

I had a similar query which returned the finishes as new columns ( f1,f2,f3 ) But it would still return 2 rows in the given example with f1 defined in the first row and f1 as null in the second row and f2 being defined in the second. 我有一个类似的查询返回完成为新列( f1,f2,f3 )但它仍然会在给定的示例中返回2行,其中f1在第一行中定义, f1在第二行中为null, f2在第二。

How can I get that into one row? 我怎么能把它变成一行?

Hopefully that makes sense, anyone have any suggestions? 希望这是有道理的,任何人都有任何建议吗?

If you need anymore info, let me know. 如果您需要更多信息,请告诉我们。

Thanks! 谢谢!

EDIT: 编辑:

The pj_items table may have duplicate entries and I need to preserve those as separate rows. pj_items表可能有重复的条目,我需要将它们保存为单独的行。 I just need to have all the finishes for each part in one row with its respective part. 我只需要将每个部件的所有表面处理与其各自的部分连在一起。

Example
Here is an example of the pj_items table: 以下是pj_items表的示例:
pj_items表

Here is the relevant information from the PartFinishes table: 以下是PartFinishes表中的相关信息:
PartFinishes表

And here is the result using the original query: 这是使用原始查询的结果:
结果

What I need in the result is the two rows of 99401326 are one with both of the finishes in that row, while maintaining the 2 seperate rows for the 99401077 since I want 2 labels of those. 我在结果中需要的是两行99401326是一行同时包含该行中的两个完成,同时保持99401077的两个单独的行,因为我想要2个标签。

You can use GROUP_CONCAT to combine all finish ID's for a given part into one column. 您可以使用GROUP_CONCAT将给定零件的所有完成ID合并为一列。

SELECT pj_items.part_num, 
       pj_items.qty, 
       GROUP_CONCAT(PartFinishes.FinishId) as FinishIds
FROM pj_items
JOIN PartFinishes 
  ON PartFinishes.PartNumber = pj_items.part_num
WHERE job_id = 1
GROUP BY PartFinishes.PartNumber

@comment: However if you want to keep rows from pj_items separated you will need to join PartFinishes already grouped by PartNumber: @comment:但是如果你想保持pj_items中的行分开,你需要加入已经按PartNumber分组的PartFinishes:

SELECT pj_items.part_num, 
   pj_items.qty, 
   FinishesGrouped.FinishIds
FROM pj_items
JOIN 
  ( SELECT PartNumber, GROUP_CONCAT(FinishId) as FinishIds
    FROM PartFinishes
    GROUP BY PartNumber ) FinishesGrouped
ON
  pj_items.part_num = FinishesGrouped.PartNumber

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

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