gpt4 book ai didi

mysql - 这可以在MYSQL中完成吗

转载 作者:行者123 更新时间:2023-11-30 22:21:45 28 4
gpt4 key购买 nike

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

这里是表的描述:运行

+----------------------+--------------+------+-----+-------------------+-----------------------------+
| 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 | |
+----------------------+--------------+------+-----+-------------------+-----------------------------+

运行项:

+--------------+-------------+------+-----+-------------------+-----------------------------+
| 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 指向错误的运行。下表是 Runs 的镜 header value 为零的表,这意味着它们没有指向它们的 runItems。所以我需要的是,当 RunItem WorkCenter 和 Run WorkCenter 不匹配时,抓取 LegacyRun(RunId) 和 Legacy RunShift,将所有 RunItems 指向 workCenters 与 Run 不匹配的地方具有相同的 LegacyRun(Id) 和相同的 Shift.Description 以及 LegacyRun。

更新_2:让我们从最上面的表中取出第一个

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

注意 legacyRun(Id) 是 21280碰巧底部表的顶行也有 21280 的 LegacyRun

如果我们从 RunItem 选择 ItemId,WorkCenter where Run = 39550(从 runItem workcenter 不计算运行的顶层表运行);我们得到:

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

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

所以为了修复这一行,我们这样做:

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com