用两个SELECTS(而不是四个)更新临时表列

时间:2019-11-13 14:58:03

标签: mysql

我想通过一次选择更新我的min_odo和min_odo_pagal_virtual。在MySQL 5.1.41中可以吗?

(min_odo, min_odo_pagal_virtual) = (SELECT min_odo, min_odo_pagal_virtual
        FROM km_statistika
        WHERE km_statistika.masinos_id = km_statistika_tmp_for_min_max_odo.masinos_id AND min_odo != '0' AND km_statistika.data BETWEEN '$nuo_data_paieskai' AND '$iki_data_paieskai' ORDER BY km_statistika.data ASC LIMIT 1)

完整代码:

UPDATE km_statistika_tmp_for_min_max_odo SET min_odo = 
        (SELECT min_odo
        FROM km_statistika
        WHERE km_statistika.masinos_id = km_statistika_tmp_for_min_max_odo.masinos_id AND min_odo != '0' AND km_statistika.data BETWEEN '$nuo_data_paieskai' AND '$iki_data_paieskai' ORDER BY km_statistika.data ASC LIMIT 1)
        , min_odo_pagal_virtual = 
        (SELECT min_odo_pagal_virtual
        FROM km_statistika
        WHERE km_statistika.masinos_id = km_statistika_tmp_for_min_max_odo.masinos_id AND min_odo != '0' AND km_statistika.data BETWEEN '$nuo_data_paieskai' AND '$iki_data_paieskai' ORDER BY km_statistika.data ASC LIMIT 1)
        , max_odo = 
        (SELECT max_odo
        FROM km_statistika
        WHERE km_statistika.masinos_id = km_statistika_tmp_for_min_max_odo.masinos_id AND max_odo != '0' AND km_statistika.data BETWEEN '$nuo_data_paieskai' AND '$iki_data_paieskai' ORDER BY km_statistika.data DESC LIMIT 1)
        , max_odo_pagal_virtual = 
        (SELECT max_odo_pagal_virtual
        FROM km_statistika
        WHERE km_statistika.masinos_id = km_statistika_tmp_for_min_max_odo.masinos_id AND min_odo != '0' AND km_statistika.data BETWEEN '$nuo_data_paieskai' AND '$iki_data_paieskai' ORDER BY km_statistika.data DESC LIMIT 1);

这是我创建tmp表的方式:

CREATE TEMPORARY TABLE `km_statistika_tmp_for_min_max_odo` (masinos_id INT(11), min_odo INT(11), min_odo_pagal_virtual INT(11), max_odo INT(11), max_odo_pagal_virtual INT(11)) SELECT masinos_id, '' AS min_odo, '' AS min_odo_pagal_virtual, '' AS max_odo, '' AS max_odo_pagal_virtual FROM km_statistika WHERE data BETWEEN '$nuo_data_paieskai' AND '$iki_data_paieskai' GROUP BY masinos_id;

1 个答案:

答案 0 :(得分:0)

对您而言,代码似乎需要每个masinos_id的更新以及temp_table的相关值 如果是这样,则可以使用JOIN

更新
  UPDATE km_statistika_tmp_for_min_max_odo  km1
  INNER JOIN (
        select masinos_id, min_date, min_odo, max_odo, min_odo_pagal_virtual, max_odo_pagal_virtual
        from km_statistika  k 
        INNER JOIN  (
          SELECT masinos_id, min( data) min_data 
            FROM km_statistika  
            WHERE  min_odo != '0' 
            AND km_statistika.data 
            BETWEEN '$nuo_data_paieskai' 
            AND '$iki_data_paieskai' 
            GROUP BY masinos_id
        ) t on t.masinos_id = k.masinos_id 
            AND t.min_data = k.data
      )  km2 ON km1.masinos_id  = km2.masinos_id


  SET km1.min_odo = km2.min_odo,
      km1.max_odo = km2.max_odo,
      km1.min_odo_pagal_virtual = km2.min_odo_pagal_virtual,
      km1.max_odo_pagal_virtual = km2.max_odo_pagal_virtual,

无论如何,您都应该避免在sql中使用php var,否则您有被sql抛弃的风险。.请确保正确清理var的内容,并应使用预处理语句和绑定参数