可以在MYSQL中完成

时间:2016-04-07 20:37:11

标签: mysql database join

我有一个名为Run的Parent表,它将Shifts作为子外键,这些转换在其表中有描述。我需要在查询中选择子班次的描述,并访问该班次的描述以与另一班次进行比较。

以下是表格说明: 运行

+----------------------+--------------+------+-----+-------------------+-----------------------------+
| Field                | Type         | Null | Key | Default           | Extra                       |
+----------------------+--------------+------+-----+-------------------+-----------------------------+
| RunId                | int(11)      | NO   | PRI | NULL              | auto_increment              |
| RunType              | varchar(1)   | YES  |     | NULL              |                             |
| Plant                | varchar(2)   | NO   | MUL | NULL              |                             |
| Warehouse            | varchar(20)  | YES  |     | NULL              |                             |
| LegacyId             | int(11)      | YES  |     | NULL              |                             |
| RunDate              | date         | NO   |     | NULL              |                             |
| Shift                | varchar(20)  | NO   |     | NULL              |                             |
| NumEmployees         | int(11)      | YES  |     | NULL              |                             |
| DryersRan            | tinyint(1)   | YES  |     | NULL              |                             |
| HogfuelDelivered     | int(11)      | YES  |     | NULL              |                             |
| ScheduledStart       | datetime     | YES  |     | NULL              |                             |
| ScheduledEnd         | datetime     | YES  |     | NULL              |                             |
| ScheduledHours       | decimal(4,2) | YES  |     | NULL              |                             |
| Downtime             | int(11)      | YES  |     | NULL              |                             |
| ProductLength        | varchar(10)  | YES  |     | NULL              |                             |
| ProductWidth         | varchar(10)  | YES  |     | NULL              |                             |
| ProductThickness     | varchar(10)  | YES  |     | NULL              |                             |
| Species              | varchar(10)  | YES  | MUL | NULL              |                             |
| NumBlocks            | int(11)      | YES  |     | NULL              |                             |
| TestRun              | tinyint(1)   | YES  |     | NULL              |                             |
| AveDiameter          | decimal(5,2) | YES  |     | NULL              |                             |
| BlockScale           | int(11)      | YES  |     | NULL              |                             |
| LogScale             | int(11)      | YES  |     | NULL              |                             |
| NumSpinouts          | int(11)      | YES  |     | NULL              |                             |
| Pieces               | int(11)      | YES  |     | NULL              |                             |
| Footage              | int(11)      | YES  |     | NULL              |                             |
| Surface              | int(11)      | YES  |     | NULL              |                             |
| Coreline             | int(11)      | YES  |     | NULL              |                             |
| Dryer1               | int(11)      | YES  |     | NULL              |                             |
| Dryer2               | int(11)      | YES  |     | NULL              |                             |
| Dryer3               | int(11)      | YES  |     | NULL              |                             |
| Sander               | int(11)      | YES  |     | NULL              |                             |
| Redry                | int(11)      | YES  |     | NULL              |                             |
| GradeStamped         | tinyint(1)   | YES  |     | NULL              |                             |
| Status               | varchar(10)  | YES  |     | NULL              |                             |
| Notes                | text         | YES  |     | NULL              |                             |
| InventoryTransaction | int(11)      | YES  |     | NULL              |                             |
| LastEdited           | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| LastEditedBy         | varchar(20)  | YES  |     | NULL              |                             |
| LegacyRun            | int(11)      | YES  |     | NULL              |                             |
| WorkCenter           | varchar(20)  | YES  | MUL | NULL              |                             |
+----------------------+--------------+------+-----+-------------------+-----------------------------+

RunItem:

+--------------+-------------+------+-----+-------------------+-----------------------------+
| Field        | Type        | Null | Key | Default           | Extra                       |
+--------------+-------------+------+-----+-------------------+-----------------------------+
| ItemId       | int(11)     | NO   | PRI | NULL              | auto_increment              |
| ItemType     | varchar(2)  | NO   |     | NULL              |                             |
| Run          | int(11)     | YES  | MUL | NULL              |                             |
| LegacyRun    | int(11)     | YES  |     | NULL              |                             |
| WorkCenter   | varchar(20) | YES  |     | NULL              |                             |
| Product      | int(11)     | YES  | MUL | NULL              |                             |
| DayProduced  | date        | YES  |     | NULL              |                             |
| Source       | varchar(20) | YES  |     | NULL              |                             |
| SourceShift  | varchar(20) | YES  |     | NULL              |                             |
| PieceCount   | int(11)     | YES  |     | NULL              |                             |
| Surface      | int(11)     | YES  |     | NULL              |                             |
| Coreline     | int(11)     | YES  |     | NULL              |                             |
| Footage      | int(11)     | YES  |     | NULL              |                             |
| LastEdited   | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| LastEditedBy | varchar(20) | YES  |     | NULL              |                             |
| Bundle       | int(11)     | YES  | MUL | NULL              |                             |
+--------------+-------------+------+-----+-------------------+-----------------------------+

这是我迄今为止尝试过的查询,它不会让我这样做(i.Shift iss)如何做到这一点?

update RunItem i, Run r, i.Shift iss, r.Shift rs 
set i.ItemId = (SELECT RunId from Run r where r.LegacyRun = i.LegacyRun and 
rs.Description
 = is.Description
and r.WorkCenter != i.WorkCenter;

给出了这个错误:

ERROR 1146 (42S02): Table 'i.Shift' doesn't exist

更新:

select r.RunId, i.Run, r.WorkCenter as runWork, i.WorkCenter as itemWork,
 r.Footage, i.LegacyRun as itemLegacy from Run r, RunItem i where r.RunId = i.Run
 and r.WorkCenter != i.WorkCenter and RunDate >= '2016-02-01' and RunDate <= '2016-02-29';

| 39550 | 39550 | P3-LAYUP      | P3-SPREADER |  260818 |      21280 |
| 39553 | 39553 | P3-LAYUP      | P3-SPREADER |  267421 |      21281 |
| 39566 | 39566 | P3-8FTSAWLINE | P3-SPREADER |  351547 |      21286 |
| 39569 | 39569 | P3-8FTSAWLINE | P3-SPREADER |  527049 |      21287 |
| 39605 | 39605 | P3-8FTSAWLINE | P3-SPREADER |  460826 |      21316 |
| 39605 | 39605 | P3-8FTSAWLINE | P3-SPREADER |  460826 |      21316 |
| 39605 | 39605 | P3-8FTSAWLINE | P3-SPREADER |  460826 |      21316 |
| 39608 | 39608 | P3-8FTSAWLINE | P3-SPREADER |  458272 |      21317 |
| 39625 | 39625 | P3-8FTSAWLINE | P3-SPREADER |  503324 |      21327 |
| 39628 | 39628 | P3-LAYUP      | P3-SPREADER |  339615 |      21328 |
| 39628 | 39628 | P3-LAYUP      | P3-SPREADER |  339615 |      21328 |
+-------+-------+---------------+-------------+---------+------------+

select RunId, WorkCenter, Shift,
 LegacyRun from Run where Footage is null
 and RunDate >='2016-02-01' and RunDate <='2016-02-29';


| 39552 | P3-SPREADER   | P3-DAY-SPREAD   |     21280 |
| 39555 | P3-SPREADER   | P3-SWING-SPREAD |     21281 |
| 39568 | P3-SPREADER   | P3-DAY-SPREAD   |     21286 |
| 39571 | P3-SPREADER   | P3-SWING-SPREAD |     21287 |
| 39607 | P3-SPREADER   | P3-DAY-SPREAD   |     21316 |
| 39609 | P3-SPREADER   | P3-SWING-SPREAD |     21317 |
| 39626 | P3-SPREADER   | P3-DAY-SPREAD   |     21327 |
| 39629 | P3-SPREADER   | P3-SWING-SPREAD |     21328 |
| 39647 | P3-8FTSAWLINE | P3-DAY-SL       |     21339 |
| 39649 | P3-8FTSAWLINE | P3-SWING-SL     |     21340 |
+-------+---------------+-----------------+-----------+

在上表中,我选择RunItem WorkCenter和Run WorkCenter不匹配的运行,这告诉我在该运行下 一些RunItems指向错误的运行。下表是表,其中运行的素材值为零,这意味着它们没有指向它们的runItems。 所以我需要的是当RunItem WorkCenter和Run WorkCenter不匹配时抓住LegacyRun(RunId)和Legacy RunShift,指向workCenters与Run不匹配的所有RunItems 具有相同的LegacyRun(Id)和相同的Shift.Description以及LegacyRun。

UPDATE_2: 让我们从顶部表中获取第一个

| 39550 | 39550 | P3-LAYUP      | P3-SPREADER |  260818 |      21280 |

注意legacyRun(Id)是21280 它只是如此发生,底部表格的顶行也有LegacyRun 21280

如果我们从RunItem选择ItemId,WorkCenter,其中Run = 39550(从runItem工作中心运行的顶层表运行); 我们得到:

+--------+-------------+
| ItemId | WorkCenter  |
+--------+-------------+
| 110336 | P3-LAYUP    |
| 110344 | P3-SPREADER |
+--------+-------------+

我们知道我们必须将带有spreader的WorkCenter的runItem指向底层表的第一行,该行没有指向它的runItems。

所以要解决这一行:

update RunItem set Run = 39552 where ItemId = 110344;

我可以通过手动执行此操作,但我想要一些可以更新所有行的内容

1 个答案:

答案 0 :(得分:0)

我得到了我需要的查询,感谢@Barmar:

UPDATE RunItem i JOIN Run r 
ON r.LegacyRun = i.LegacyRun 
AND r.WorkCenter = i.WorkCenter 
JOIN Run rr 
ON rr.RunId = i.Run 
SET  i.Run = r.RunId 
where i.WorkCenter != rr.WorkCenter;

添加JOIN ON后,这是一个更简单,更优雅的查询。