我有以下几组数据…
状态监测位置数据(CML表)
+--------+--------------------+--------------------+--------------+------------+--------------------+-------+-----+
| CML_ID | POF_COLUMN | CML_TYPE | SAMPLE_VALUE | COMPLIANCE | CORROSION_SEVERITY | LR_LD | POF |
+--------+--------------------+--------------------+--------------+------------+--------------------+-------+-----+
| 1 | SAMPLE_VALUE | MIC_SAMPLING_POINT | 5 | NO | MINOR | 1 | |
| 2 | SAMPLE_VALUE | MIC_SAMPLING_POINT | 0.5 | NO | MINOR | 2 | |
| 3 | SAMPLE_VALUE | MIC_SAMPLING_POINT | 20 | NO | MINOR | 3 | |
| 4 | COMPLIANCE | VALVE_ROTATED | 0 | YES | MINOR | 4 | |
| 5 | LR_LD | PIPING_THICKNESS | 0 | YES | MINOR | 0.1 | |
| 6 | CORROSION_SEVERITY | VESSEL_SHELL | 0 | NO | SEVERE | 0 | |
| 7 | CORROSION_SEVERITY | NOZZLE | 0 | NO | LOW | 0 | |
+--------+--------------------+--------------------+--------------+------------+--------------------+-------+-----+
失效概率查找数据(POF表)
+--------------------+--------------------+-------------+-----+
| POF_COLUMN | CML_TYPE | VALUE_RANGE | POF |
+--------------------+--------------------+-------------+-----+
| SAMPLE_VALUE | MIC_SAMPLING_POINT | 1 | 5 |
| SAMPLE_VALUE | MIC_SAMPLING_POINT | 5 | 4 |
| SAMPLE_VALUE | MIC_SAMPLING_POINT | 10 | 3 |
| SAMPLE_VALUE | MIC_SAMPLING_POINT | 15 | 2 |
| SAMPLE_VALUE | MIC_SAMPLING_POINT | 100 | 1 |
| COMPLIANCE | VALVE_ROTATED | YES | 5 |
| COMPLIANCE | VALVE_ROTATED | NO | 1 |
| LR_LD | PIPING_THICKNESS | 2 | 5 |
| LR_LD | PIPING_THICKNESS | 1.5 | 4 |
| LR_LD | PIPING_THICKNESS | 1 | 3 |
| LR_LD | PIPING_THICKNESS | 0.8 | 2 |
| LR_LD | PIPING_THICKNESS | 0.5 | 1 |
| CORROSION_SEVERITY | VESSEL_SHELL | NEGLIGIBLE | 5 |
| CORROSION_SEVERITY | VESSEL_SHELL | LOW | 4 |
| CORROSION_SEVERITY | VESSEL_SHELL | MEDIUM | 3 |
| CORROSION_SEVERITY | VESSEL_SHELL | HIGH | 2 |
| CORROSION_SEVERITY | VESSEL_SHELL | SEVERE | 1 |
| CORROSION_SEVERITY | NOZZLE | NEGLIGIBLE | 5 |
| CORROSION_SEVERITY | NOZZLE | LOW | 5 |
| CORROSION_SEVERITY | NOZZLE | MEDIUM | 5 |
| CORROSION_SEVERITY | NOZZLE | HIGH | 3 |
| CORROSION_SEVERITY | NOZZLE | SEVERE | 2 |
+--------------------+--------------------+-------------+-----+
我需要使用[pof_列]、[cml_type]和[sample_value]、[compliance]、[corrosion_severity]或[lr_ld]字段返回文件柜或与pof表中的记录完全匹配的记录。然后更新记录pof到cml表。
例如,如果我们看cml_id=2。
按[POF_column]=“sample_value”、[CML_type]=“mic_sampling_point”和[value_range]筛选POF表,[sample_value]列中的值(在本例中为0.5)是最小值。
在这种情况下,它将匹配pof表中的第一条记录,并返回一个pof=5的值。
如果我们再看一个案子。CML_id=7.
按[pof_列]=腐蚀严重度,[cml_类型]=喷嘴和[value_range]=腐蚀严重度列中的值过滤pof表,在本例中为“低”。
在这种情况下,它将匹配pof表底部的第四行,并返回一个pof=5的值。
总之,我需要更新cml表以显示以下结果…
+--------+---+-----+
| CML_ID | … | POF |
+--------+---+-----+
| 1 | … | 4 |
| 2 | … | 5 |
| 3 | … | 2 |
| 4 | … | 5 |
| 5 | … | 1 |
| 6 | … | 1 |
| 7 | … | 5 |
+--------+---+-----+
有人知道我怎样才能做到这一点吗?我在下面放了一些我尝试的示例代码。用于查找精确匹配值,但不查找最接近的匹配值。
DROP DATABASE IF EXISTS VESSELS;
CREATE DATABASE VESSELS;
USE VESSELS;
CREATE TABLE CML (
`CML_ID` INTEGER NOT NULL,
`POF_COLUMN` VARCHAR(50),
`CML_TYPE` VARCHAR(50),
`SAMPLE_VALUE` FLOAT,
`COMPLIANCE` ENUM('YES','NO'),
`CORROSION_SEVERITY` VARCHAR(50),
`LR_LD` FLOAT,
`POF` TINYINT,
PRIMARY KEY (CML_ID)
);
CREATE TABLE POF (
`POF_COLUMN` VARCHAR(50),
`CML_TYPE` VARCHAR(50),
`VALUE_RANGE` VARCHAR(100),
`POF` TINYINT,
PRIMARY KEY (POF_COLUMN, CML_TYPE, VALUE_RANGE)
);
INSERT INTO CML
VALUES (1, 'SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 5, 'NO', 'MINOR', 1, NULL),
(2, 'SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 0.5, 'NO', 'MINOR', 2, NULL),
(3, 'SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 20, 'NO', 'MINOR', 3, NULL),
(4, 'COMPLIANCE', 'VALVE_ROTATED', 0, 'YES', 'MINOR', 4, NULL),
(5, 'LR_LD', 'PIPING_THICKNESS', 0, 'YES', 'MINOR', 0.1, NULL),
(6, 'CORROSION_SEVERITY', 'VESSEL_SHELL', 0, 'NO', 'SEVERE', 0, NULL),
(7, 'CORROSION_SEVERITY', 'NOZZLE', 0, 'NO', 'LOW', 0, NULL);
INSERT INTO POF
VALUES ('SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 1, 5),
('SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 5, 4),
('SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 10, 3),
('SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 15, 2),
('SAMPLE_VALUE', 'MIC_SAMPLING_POINT', 100, 1),
('COMPLIANCE', 'VALVE_ROTATED', 'YES', 5),
('COMPLIANCE', 'VALVE_ROTATED', 'NO', 1),
('LR_LD', 'PIPING_THICKNESS', 2, 5),
('LR_LD', 'PIPING_THICKNESS', 1.5, 4),
('LR_LD', 'PIPING_THICKNESS', 1, 3),
('LR_LD', 'PIPING_THICKNESS', 0.8, 2),
('LR_LD', 'PIPING_THICKNESS', 0.5, 1),
('CORROSION_SEVERITY', 'VESSEL_SHELL', 'NEGLIGIBLE', 5),
('CORROSION_SEVERITY', 'VESSEL_SHELL', 'LOW', 4),
('CORROSION_SEVERITY', 'VESSEL_SHELL', 'MEDIUM', 3),
('CORROSION_SEVERITY', 'VESSEL_SHELL', 'HIGH', 2),
('CORROSION_SEVERITY', 'VESSEL_SHELL', 'SEVERE', 1),
('CORROSION_SEVERITY', 'NOZZLE', 'NEGLIGIBLE', 5),
('CORROSION_SEVERITY', 'NOZZLE', 'LOW', 5),
('CORROSION_SEVERITY', 'NOZZLE', 'MEDIUM', 5),
('CORROSION_SEVERITY', 'NOZZLE', 'HIGH', 3),
('CORROSION_SEVERITY', 'NOZZLE', 'SEVERE', 2);
-- UPDATE EXACT MATCHING VALUE
UPDATE CML c
LEFT JOIN POF p
ON c.POF_COLUMN = p.POF_COLUMN
AND c.CML_TYPE = p.CML_TYPE
AND ( (c.POF_COLUMN = 'COMPLIANCE' AND c.COMPLIANCE = p.VALUE_RANGE) OR
(c.POF_COLUMN = 'SAMPLE_VALUE' AND c.SAMPLE_VALUE = p.VALUE_RANGE) OR
(c.POF_COLUMN = 'LR_LD' AND c.LR_LD = p.VALUE_RANGE) OR
(c.POF_COLUMN = 'CORROSION_SEVERITY' AND c.CORROSION_SEVERITY = p.VALUE_RANGE)
)
SET c.POF = p.POF;
SELECT * FROM CML;
最佳答案
投票给一个非常详细的帖子与样本数据和预期结果。
问题在于sample_value和lr_ld,因为它可能不是pof表中的确切值。但是,您将注意到这些值等于或小于值范围。
因此,如果我们得到样本值或lr值小于或等于值范围的pof值的最大值,那么我们只需要得到pof值的最大值。
此查询将只起作用,因为当sample_value或lr_ld增加时,pof值增加。
UPDATE CML c
JOIN
(
select c.CML_ID, max(p.POF) POF
from CML c
LEFT JOIN POF p
ON c.POF_COLUMN = p.POF_COLUMN
AND c.CML_TYPE = p.CML_TYPE
AND ( (c.POF_COLUMN = 'COMPLIANCE' AND c.COMPLIANCE = p.VALUE_RANGE) OR
(c.POF_COLUMN = 'SAMPLE_VALUE' AND c.SAMPLE_VALUE<=p.VALUE_RANGE) OR
(c.POF_COLUMN = 'LR_LD' AND c.LR_LD <= p.VALUE_RANGE) OR
(c.POF_COLUMN = 'CORROSION_SEVERITY' AND c.CORROSION_SEVERITY = p.VALUE_RANGE)
)
group by c.CML_ID
) t
on c.CML_ID = t.CML_ID
set c.POF = t.POF;
关于mysql - MySQL Lookup与更改查找列的最近或精确匹配值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49268185/