如何从同一个表中的子查询更新列?

时间:2016-06-08 21:06:21

标签: mysql sql

出于缓存原因,我需要将Km(千米)字段写回位置表,到目前为止我能够计算出Km。到目前为止我尝试的所有内容(使用工会,子查询等更新)都会给出SQL错误1093.

在MySQL中,如何将Km值更新回表格?

这是表格架构

CREATE TABLE `locations` (
  `epoch_date` int(10) unsigned DEFAULT NULL,
  `latitude` float NOT NULL,
  `longitude` float NOT NULL,
  `km` float DEFAULT NULL
);

INSERT INTO `locations` (`epoch_date`, `latitude`, `longitude`, `km`) VALUES
(1429913506, -8.7285, 119.401, NULL),
(1429913631, -9.1279, 117.67, NULL),
(1429945707, -8.7063, 119.36, NULL),
(1431929523, -8.5745, 119.707, NULL),
(1431941343, -8.5773, 119.713, NULL),
(1431958383, -8.5881, 119.724, NULL),
(1431969963, -8.589, 119.728, NULL),
(1431998403, -8.5766, 119.724, NULL);

这是生成KM(公里)

的选择查询
SELECT
    latitude
    , longitude
    , epoch_date
    , @latitude2 :=
    (
        SELECT
            latitude
        FROM locations loc1
        WHERE
            loc1.epoch_date < loc.epoch_date
        ORDER BY epoch_date DESC LIMIT 1 OFFSET 0
    ) as prev_latitude
    , @longitude2 :=
    (
        SELECT
            longitude
        FROM locations loc1
        WHERE
            loc1.epoch_date < loc.epoch_date
        ORDER BY epoch_date DESC LIMIT 1 OFFSET 0
    ) as prev_longitude
    , (ACOS(COS(RADIANS(90-latitude)) *COS(RADIANS(90-@latitude2)) +SIN(RADIANS(90-latitude)) *SIN(RADIANS(90-@latitude2)) *COS(RADIANS(longitude-@longitude2))) *6371) as km
FROM locations loc
ORDER BY epoch_date

以下是SQL小提琴http://sqlfiddle.com/#!9/7f95de/2/0

的链接

2 个答案:

答案 0 :(得分:3)

首先,您无法保证在select语句中使用变量。 MySQL不保证select中表达式的排序,因此可以按任何顺序分配变量。根据评估顺序是危险的。

解决该问题后(可能使用子查询),您可以使用子查询更新km列:

update locations l join
       (<your query here>) u
       on l.latitude = u.latitude and l.longitude = u.longitude
    set l.km = u.km;

哦,我也看到了其他危险的东西。您使用浮点数表示lat和long的表示。你应该使用小数,通常像decimal(10, 6)这样的东西就足够了。

答案 1 :(得分:1)

您可以设置功能以返回prev_latitude和pre_longitude,并在UPDATE查询中使用该功能。

像这样的东西

DELIMITER $$
CREATE FUNCTION prev_latitude(epoch INT)
  RETURNS FLOAT
  LANGUAGE SQL
BEGIN
  DECLARE lat FLOAT;
  SET lat = null;

  SELECT latitude INTO lat
  FROM locations
  WHERE epoch_date < epoch
  ORDER BY epoch_date DESC LIMIT 1 OFFSET 0;

  RETURN lat;
END;
$$
DELIMITER ;

并且

DELIMITER $$
CREATE FUNCTION prev_longitude(epoch INT)
  RETURNS FLOAT
  LANGUAGE SQL
BEGIN
  DECLARE lon FLOAT;
  SET lon = null;

  SELECT longitude INTO lon
  FROM locations
  WHERE epoch_date < epoch
  ORDER BY epoch_date DESC LIMIT 1 OFFSET 0;

  RETURN lon;
END;
$$
DELIMITER ;

您的更新查询现在可以写为

UPDATE locations
SET km = (ACOS(COS(RADIANS(90-latitude)) *COS(RADIANS(90-prev_latitude(epoch_date))) +SIN(RADIANS(90-latitude)) *SIN(RADIANS(90-prev_latitude(epoch_date))) *COS(RADIANS(longitude-prev_longitude(epoch_date)))) *6371);