CREATE TABLE IF NOT EXISTS `exam_results` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_code` int(11) NOT NULL,
`total_level` int(11) NOT NULL,
`total_rank` int(11) NOT NULL,
`total_level_2` int(11) NOT NULL,
`total_rank_2` int(11) NOT NULL,
);
total_level 且 total_rank 且 total_level_2 包含值但 total_rank_2 为空
我想选择 total_level_2 的值,并在 total_level 中找到最接近的值, 然后使用找到的行的 total_rank 更新 total_rank_2
如何在查询中执行此操作?
更新
total_level | total_rank | total_level_2 | total_rank_2
--------------------------------------------------------------------------
1500 | 100 | 2000 | ?
--------------------------------------------------------------------------
1700 | 90 | 3500 | ?
--------------------------------------------------------------------------
2000 | 70 | 1500 | ?
例如,更新后的最后一行应为100。 因为最后一行中 total_level_2 的最接近值在第一行内,所以最后一行的 total_rank_2 将是第一行的 total_rank
更新后的表格:
total_level | total_rank | total_level_2 | total_rank_2
--------------------------------------------------------------------------
1500 | 100 | 2000 | 70
--------------------------------------------------------------------------
1700 | 90 | 3500 | 70
--------------------------------------------------------------------------
2000 | 70 | 1500 | 100
答案 0 :(得分:1)
试试这个:
clientCertificateMappingAuthentication
答案 1 :(得分:1)
CREATE TEMPORARY TABLE IF NOT EXISTS tempresults_cal AS (select cni.*,
(select total_rank
from exam_results ci
where ci.analyze_id="'.$Params->get('analyze_id').'"
order by abs(total_level - cni.total_level_2)
limit 1
) as new_rank
from exam_results cni;
UPDATE exam_results
INNER
JOIN tempresults_cal
ON tempresults_cal.id = exam_results.id
SET exam_results.total_rank_2 = tempresults_cal.new_rank