根据sql中另一列中的值递增值

时间:2015-01-16 11:19:12

标签: mysql sql

我有一个表包含service_id,service_name,weight_start_range,Weight_end_range和increment_by。

service_id service_name weight_start_range  Weight_end_range    increment_by
sr1        Service1           0                    500             100
sr2        Service2           200                  300              0
sr3        Service3           0                    1000            500
sr4        Service4           0                    250              0
sr5        Service5           50                   100              0

我想从这个表中获取数据并使用SQL查询创建一个表:

service_id  service_name    weight_start_range  Weight_end_range 
sr1         Service1                0               100
sr1         Service1                100             200
sr1         Service1                200             300
sr1         Service1                300             400
sr1         Service1                400             500
sr2         Service2                200             300
sr3         Service3                0               500
sr3         Service3                500             1000
sr4         Service4                0               250
sr5         Service5                50              100

有人可以帮助我吗?

我无法设计表格,因此附加图片以查看结构。enter image description here

1 个答案:

答案 0 :(得分:0)

鉴于你的要求,首先想到的是关于编写子查询,但是提供循环功能是一项复杂的任务,因此我决定为它创建程序。

数据库设置:

CREATE TABLE `servicedata` (
  `service_id` varchar(255) NOT NULL,
  `serviceName` varchar(255) DEFAULT NULL,
  `weight_start_range` double DEFAULT NULL,
  `weight_end_range` double DEFAULT NULL,
  `increment_by` double DEFAULT NULL,
  PRIMARY KEY (`service_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

SEED数据:

insert into `servicedata` (`service_id`, `serviceName`, `weight_start_range`, `weight_end_range`, `increment_by`) values('sr1','srvice1','0','500','100');
insert into `servicedata` (`service_id`, `serviceName`, `weight_start_range`, `weight_end_range`, `increment_by`) values('sr2','service2','200','300','0');
insert into `servicedata` (`service_id`, `serviceName`, `weight_start_range`, `weight_end_range`, `increment_by`) values('sr3','service3','0','1000','500');
insert into `servicedata` (`service_id`, `serviceName`, `weight_start_range`, `weight_end_range`, `increment_by`) values('sr4','service4','0','250','0');

servicedata表包含以下数据:

service_id  serviceName  weight_start_range  weight_end_range  increment_by
----------  -----------  ------------------  ----------------  ------------
sr1         srvice1                       0               500           100
sr2         service2                    200               300             0
sr3         service3                      0              1000           500
sr4         service4                      0               250             0

PROCEDURE customOutPut:

DELIMITER // 
DROP PROCEDURE IF EXISTS customOutPut//

CREATE PROCEDURE customOutPut()
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE serviceid VARCHAR(50) ;
DECLARE servicename VARCHAR(50) ;
DECLARE START INTEGER ;
DECLARE END INTEGER ;
DECLARE incrementby INTEGER ;
DECLARE WEIGHT_END INTEGER DEFAULT 0;
DECLARE endRange INTEGER;
DECLARE startRange INTEGER;

DECLARE selectCursor CURSOR FOR 
    SELECT service_id,servicename , weight_START_range , weight_end_range ,increment_by 
    FROM servicedata; 

DECLARE CONTINUE HANDLER 
FOR NOT FOUND SET v_finished = 1;

DROP TABLE IF EXISTS DATA_SPREAD_WITH_INCREMENT_BY ;
CREATE TABLE DATA_SPREAD_WITH_INCREMENT_BY(service_id VARCHAR(50) ,servicename VARCHAR(50), weight_START_range INTEGER, weight_end_range INTEGER );

OPEN selectCursor;  

getData: LOOP

FETCH selectCursor INTO serviceid,servicename,START,END,incrementby ;

IF v_finished =1 THEN 
  LEAVE getData;
END IF ; 

IF incrementby = 0 THEN
   INSERT INTO DATA_SPREAD_WITH_INCREMENT_BY VALUES (serviceid,servicename,START,END);

ELSE 
 SET endRange =START;
 SET startRange = START;
    spreadData: LOOP

    SET endRange = endRange+incrementby ;
    IF endrange > END THEN
        LEAVE spreadData;
    END IF;

    INSERT INTO DATA_SPREAD_WITH_INCREMENT_BY VALUES (serviceid,servicename,startRange,endRange);

    SET startRange = startRange+incrementby ;

    END LOOP;  

END IF ;    

END LOOP getData;

END  //
DELIMITER ;

您应该将程序称为CALL customOutPut()

在SQL

下面的data_spread_with_increment_by fire中查看数据
SELECT * FROM `data_spread_with_increment_by`

表格data_spread_with_increment_by

service_id  servicename  weight_START_range  weight_end_range
----------  -----------  ------------------  ----------------
sr1         (NULL)                        0               100
sr1         (NULL)                      100               200
sr1         (NULL)                      200               300
sr1         (NULL)                      300               400
sr1         (NULL)                      400               500
sr2         (NULL)                      200               300
sr3         (NULL)                        0               500
sr3         (NULL)                      500              1000
sr4         (NULL)                        0               250

我已经提供了这个程序作为指导,我可能已经错过了一些边缘案例,但我相信你当然可以解决它们。

请发布您的建议,此程序是否适合您。