如何将值复制10次到相邻列?

时间:2015-09-01 14:25:27

标签: mysql

我正在尝试从相邻列的前10行中的某个列复制一个值,然后对接下来的十行执行相同的操作,直到我到达结尾为止。

enter image description here

数据库以我想要复制的值开头。

这是我对表的转储:

-- phpMyAdmin SQL Dump
-- version 4.0.10.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Sep 01, 2015 at 10:07 AM
-- Server version: 5.1.73
-- PHP Version: 5.3.3

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `def_serv`
--

-- --------------------------------------------------------

--
-- Table structure for table `def_servpath_0001_weatherstation`
--

CREATE TABLE IF NOT EXISTS `def_servpath_0001_weatherstation` (
  `recvTimeTs` mediumtext,
  `recvTime` text,
  `entityId` text,
  `entityType` text,
  `attrName` text,
  `attrType` text,
  `attrValue` text,
  `attrMd` text
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `def_servpath_0001_weatherstation`
--



INSERT INTO `def_servpath_0001_weatherstation` (`recvTimeTs`, `recvTime`, `entityId`, `entityType`, `attrName`, `attrType`, `attrValue`, `attrMd`) VALUES
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'time', 'datetime', '6:30:0,1.9.2015', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'at', 'double', '18.799', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'ah', 'double', '91.0', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'ws', 'double', '0.01', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'wd', 'int', '270', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'sr', 'double', '17.181', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'sm1', 'double', '24.683', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'sp', 'double', '17.181', '[]'),
('1441083706', '2015-09-01T07:01:46.104', '0001', 'WeatherStation', 'bt', 'double', '6.552', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'time', 'datetime', '6:45:0,1.9.2015', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'at', 'double', '18.799', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'ah', 'double', '91.0', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'ws', 'double', '0.0', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'wd', 'int', '270', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'sr', 'double', '17.898', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'sm1', 'double', '24.683', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'sp', 'double', '17.898', '[]'),
('1441083713', '2015-09-01T07:01:53.549', '0001', 'WeatherStation', 'bt', 'double', '6.591', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'time', 'datetime', '7:0:0,1.9.2015', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'at', 'double', '19.400', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'ah', 'double', '91.0', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'ws', 'double', '0.0', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'wd', 'int', '270', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'sr', 'double', '18.488', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'sm1', 'double', '24.683', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'sp', 'double', '18.488', '[]'),
('1441083720', '2015-09-01T07:02:00.951', '0001', 'WeatherStation', 'bt', 'double', '6.640', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'time', 'datetime', '7:15:0,1.9.2015', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'at', 'double', '19.400', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'ah', 'double', '91.0', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'ws', 'double', '0.59', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'wd', 'int', '236', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'sr', 'double', '18.801', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'sm1', 'double', '24.386', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'sp', 'double', '18.801', '[]'),
('1441087323', '2015-09-01T08:02:03.349', '0001', 'WeatherStation', 'bt', 'double', '6.689', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'time', 'datetime', '7:30:0,1.9.2015', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'at', 'double', '20.0', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'ah', 'double', '90.0', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'ws', 'double', '0.35', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'wd', 'int', '290', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'sr', 'double', '18.894', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'sm1', 'double', '24.683', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'sp', 'double', '18.894', '[]'),
('1441087330', '2015-09-01T08:02:10.383', '0001', 'WeatherStation', 'bt', 'double', '6.757', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'time', 'datetime', '7:45:0,1.9.2015', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'at', 'double', '20.5', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'ah', 'double', '90.0', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'ws', 'double', '0.37', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'wd', 'int', '322', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'sr', 'double', '19.298', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'sm1', 'double', '24.683', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'sp', 'double', '19.298', '[]'),
('1441087338', '2015-09-01T08:02:18.889', '0001', 'WeatherStation', 'bt', 'double', '6.865', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'time', 'datetime', '8:0:0,1.9.2015', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'at', 'double', '21.099', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'ah', 'double', '88.0', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'ws', 'double', '0.35', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'wd', 'int', '290', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'sr', 'double', '19.391', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'sm1', 'double', '24.386', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'sp', 'double', '19.391', '[]'),
('1441087346', '2015-09-01T08:02:26.037', '0001', 'WeatherStation', 'bt', 'double', '6.923', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'time', 'datetime', '8:15:0,1.9.2015', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'at', 'double', '21.599', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'ah', 'double', '85.0', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'ws', 'double', '0.52', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'wd', 'int', '282', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'sr', 'double', '19.548', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'sm1', 'double', '24.386', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'sp', 'double', '19.548', '[]'),
('1441090890', '2015-09-01T09:01:30.186', '0001', 'WeatherStation', 'bt', 'double', '6.962', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'time', 'datetime', '8:30:0,1.9.2015', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'at', 'double', '22.200', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'ah', 'double', '84.0', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'ws', 'double', '0.98', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'wd', 'int', '58', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'sr', 'double', '19.796', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'sm1', 'double', '24.386', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'sp', 'double', '19.796', '[]'),
('1441090898', '2015-09-01T09:01:38.072', '0001', 'WeatherStation', 'bt', 'double', '7.011', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'time', 'datetime', '8:45:0,1.9.2015', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'at', 'double', '23.299', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'ah', 'double', '80.0', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'ws', 'double', '0.27', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'wd', 'int', '242', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'sr', 'double', '19.796', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'sm1', 'double', '24.386', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'sp', 'double', '19.796', '[]'),
('1441090905', '2015-09-01T09:01:45.32', '0001', 'WeatherStation', 'bt', 'double', '7.118', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'time', 'datetime', '9:0:0,1.9.2015', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'at', 'double', '24.400', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'ah', 'double', '77.0', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'ws', 'double', '0.22', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'wd', 'int', '270', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'sr', 'double', '20.138', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'sm1', 'double', '24.386', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'sp', 'double', '20.138', '[]'),
('1441090913', '2015-09-01T09:01:53.36', '0001', 'WeatherStation', 'bt', 'double', '7.060', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'time', 'datetime', '9:15:0,1.9.2015', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'at', 'double', '25.5', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'ah', 'double', '68.0', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'ws', 'double', '0.65', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'wd', 'int', '280', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'pp', 'double', '0.0', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'sr', 'double', '20.170', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'sm1', 'double', '24.386', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'lw1', 'double', '0.0', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'sp', 'double', '20.170', '[]'),
('1441094510', '2015-09-01T10:01:50.44', '0001', 'WeatherStation', 'bt', 'double', '7.050', '[]');

这对我来说似乎是一个非常复杂的问题,我甚至无法开始如何撰写它。

有人有任何建议吗?

1 个答案:

答案 0 :(得分:1)

我已根据您的数据编写了一个查询供您更新。我希望它能解决你的问题。如果您要在实时服务器上执行它,请不要忘记保留原始数据的备份:

<强>查询

UPDATE def_servpath_0001_weatherstation AS t1
        LEFT JOIN
    (SELECT 
        *
    FROM
        def_servpath_0001_weatherstation
    GROUP BY recvTimeTs , entityId) AS t2 ON t1.recvTimeTs = t2.recvTimeTs 
SET 
    t1.attrValue = t2.attrValue;