我有一个称为“运行”的父表,该表将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不匹配的运行,这告诉我在该Run下
一些RunItem指向错误的运行。下表是“跑步次数”为零的表,这表示它们没有指向它们的runItem。
所以我需要的是,当RunItem WorkCenter和Run WorkCenter不匹配时,请抓住LegacyRun(RunId),然后将该Legacy RunShift指向所有WorkCenter与之不匹配的RunItem。
具有相同的LegacyRun(Id)和相同的Shift.Description和LegacyRun。
UPDATE_2:
让我们从最上面的桌子拿第一个
| 39550 | 39550 | P3-LAYUP | P3-SPREADER | 260818 | 21280 |
请注意,legacyRun(Id)为21280
碰巧的是,底部表格的顶部行也具有LegacyRun 21280
如果我们选择ItemId,则从RunItem中选择WorkCenter,其中Run = 39550(从顶层表中运行,其中runItem工作中心不对运行进行数学计算);
我们得到:
+--------+-------------+
| ItemId | WorkCenter |
+--------+-------------+
| 110336 | P3-LAYUP |
| 110344 | P3-SPREADER |
+--------+-------------+
我们知道必须将带有吊具工作中心的runItem指向底部表的第一行,该行没有runItem指向该行。
因此,要修复这一行,我们要做:
update RunItem set Run = 39552 where ItemId = 110344;
我可以手动进行操作,但是我想要可以更新所有行的内容
最佳答案
感谢@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
,这将是一个更简单,更优雅的查询。关于mysql - 可以在MYSQL中完成吗,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36486728/