如何编写MySql存储过程以显示每个车辆ID的所有原因

时间:2015-09-02 05:47:27

标签: mysql stored-procedures

enter image description here 我希望显示每个车辆ID近27个理由,,但我的代码仅针对车辆ID 1显示,低于我想要的2,3,4。 ... N。 我想帮助嵌套while循环。 例如 - 我有27个理由和40辆车,然后每辆车需要显示27个无人机。即结果应为27x30 = 810行

我附上了我的查询结果在img

中显示的内容
DELIMITER $$
DROP PROCEDURE IF EXISTS `searchvehicle`.`reason`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `reason`()
BEGIN
DECLARE vid_t,resnid_t,totkm_t,store_t,pump_t,other_t INT;
DECLARE vnostring_t VARCHAR(30);
DECLARE resn_t VARCHAR(100);
DECLARE indate_t DATE DEFAULT FALSE;
DECLARE done1 INT DEFAULT FALSE;
DECLARE done INT DEFAULT FALSE; 
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE m INT DEFAULT 0;
DECLARE j INT DEFAULT 0;
DECLARE CUR_V CURSOR FOR SELECT vid,vnostring FROM vehicle_owner;
DECLARE CUR_RESN CURSOR FOR SELECT resnid,resn FROM usedfor;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;   
DROP TABLE IF EXISTS allvehicle;
CREATE TABLE allvehicle
 (
  id int not null auto_increment primary key, 
  resnid int,
  resn varchar(100),
  vid int,
  vnostring varchar(30),
  indate date,
  totkm int,
  store int,
  pump int,
  other int
  );    
 OPEN CUR_RESN;
 OPEN CUR_V;
 READ_LOOP_R: LOOP      
 READ_LOOP_V:LOOP  
 SELECT COUNT(*) FROM vehicle_owner INTO m;
 SET j=0;    
 WHILE j<m DO 
 FETCH CUR_V INTO vid_t,vnostring_t; 
 IF done THEN LEAVE READ_LOOP_V;
 END IF; 
 SELECT COUNT(*) FROM usedfor INTO n;
 SET i=0;       
 WHILE i<n DO            
 FETCH CUR_RESN INTO resnid_t,resn_t;  
 IF done THEN LEAVE READ_LOOP_R;
 END IF; 
 INSERT INTO allvehicle(vid,vnostring,resnid,resn)values
 (vid_t,vnostring_t,resnid_t,resn_t);    
 SET i = i + 1; 
 END WHILE;          
 SET j = j + 1;           
 END WHILE;      
 END LOOP; 
 END LOOP;     
 CLOSE CUR_RESN;
 CLOSE CUR_V;
 select * from allvehicle;
END$$
 DELIMITER ;

1 个答案:

答案 0 :(得分:0)

有一种特定的连接类型,它强制将一个表的所有行应用于另一个表的所有行(&#34;笛卡尔积&#34;)。这是CROSS JOIN,它的用法如下:

select
      r.*, v.*
from reasons r
cross join allvehicles v

注意:如果您在问题中提供了样本数据和预期结果,则可以生成更详细的查询。

SQL Fiddle

MySQL 5.6架构设置

    CREATE TABLE Reasons
    (`id` int)
    ;

    INSERT INTO Reasons
    (`id`)
    VALUES
    (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27)
    ;

    CREATE TABLE allVehicles
    (`vid` int)
    ;

    INSERT INTO allVehicles
    (`vid`)
    VALUES
    (100),(200),(300),(400),(500),(600),(700),(800),(900),(1000)
    ;

查询1

select
      r.*, v.*
from reasons r
cross join allvehicles v
order by
    v.vid, r.id

<强> Results

| id |  vid |
|----|------|
|  1 |  100 |
|  2 |  100 |
|  3 |  100 |
|  4 |  100 |
|  5 |  100 |
|  6 |  100 |
|  7 |  100 |
|  8 |  100 |
|  9 |  100 |
| 10 |  100 |
| 11 |  100 |
| 12 |  100 |
| 13 |  100 |
| 14 |  100 |
| 15 |  100 |
| 16 |  100 |
| 17 |  100 |
| 18 |  100 |
| 19 |  100 |
| 20 |  100 |
| 21 |  100 |
| 22 |  100 |
| 23 |  100 |
| 24 |  100 |
| 25 |  100 |
| 26 |  100 |
| 27 |  100 |
|  1 |  200 |
|  2 |  200 |
|  3 |  200 |
|  4 |  200 |
|  5 |  200 |
|  6 |  200 |
|  7 |  200 |
|  8 |  200 |
|  9 |  200 |
| 10 |  200 |
| 11 |  200 |
| 12 |  200 |
| 13 |  200 |
| 14 |  200 |
| 15 |  200 |
| 16 |  200 |
| 17 |  200 |
| 18 |  200 |
| 19 |  200 |
| 20 |  200 |
| 21 |  200 |
| 22 |  200 |
| 23 |  200 |
| 24 |  200 |
| 25 |  200 |
| 26 |  200 |
| 27 |  200 |
|  1 |  300 |
|  2 |  300 |
|  3 |  300 |
|  4 |  300 |
|  5 |  300 |
|  6 |  300 |
|  7 |  300 |
|  8 |  300 |
|  9 |  300 |
| 10 |  300 |
| 11 |  300 |
| 12 |  300 |
| 13 |  300 |
| 14 |  300 |
| 15 |  300 |
| 16 |  300 |
| 17 |  300 |
| 18 |  300 |
| 19 |  300 |
| 20 |  300 |
| 21 |  300 |
| 22 |  300 |
| 23 |  300 |
| 24 |  300 |
| 25 |  300 |
| 26 |  300 |
| 27 |  300 |
|  1 |  400 |
|  2 |  400 |
|  3 |  400 |
|  4 |  400 |
|  5 |  400 |
|  6 |  400 |
|  7 |  400 |
|  8 |  400 |
|  9 |  400 |
| 10 |  400 |
| 11 |  400 |
| 12 |  400 |
| 13 |  400 |
| 14 |  400 |
| 15 |  400 |
| 16 |  400 |
| 17 |  400 |
| 18 |  400 |
| 19 |  400 |
| 20 |  400 |
| 21 |  400 |
| 22 |  400 |
| 23 |  400 |
| 24 |  400 |
| 25 |  400 |
| 26 |  400 |
| 27 |  400 |
|  1 |  500 |
|  2 |  500 |
|  3 |  500 |
|  4 |  500 |
|  5 |  500 |
|  6 |  500 |
|  7 |  500 |
|  8 |  500 |
|  9 |  500 |
| 10 |  500 |
| 11 |  500 |
| 12 |  500 |
| 13 |  500 |
| 14 |  500 |
| 15 |  500 |
| 16 |  500 |
| 17 |  500 |
| 18 |  500 |
| 19 |  500 |
| 20 |  500 |
| 21 |  500 |
| 22 |  500 |
| 23 |  500 |
| 24 |  500 |
| 25 |  500 |
| 26 |  500 |
| 27 |  500 |
|  1 |  600 |
|  2 |  600 |
|  3 |  600 |
|  4 |  600 |
|  5 |  600 |
|  6 |  600 |
|  7 |  600 |
|  8 |  600 |
|  9 |  600 |
| 10 |  600 |
| 11 |  600 |
| 12 |  600 |
| 13 |  600 |
| 14 |  600 |
| 15 |  600 |
| 16 |  600 |
| 17 |  600 |
| 18 |  600 |
| 19 |  600 |
| 20 |  600 |
| 21 |  600 |
| 22 |  600 |
| 23 |  600 |
| 24 |  600 |
| 25 |  600 |
| 26 |  600 |
| 27 |  600 |
|  1 |  700 |
|  2 |  700 |
|  3 |  700 |
|  4 |  700 |
|  5 |  700 |
|  6 |  700 |
|  7 |  700 |
|  8 |  700 |
|  9 |  700 |
| 10 |  700 |
| 11 |  700 |
| 12 |  700 |
| 13 |  700 |
| 14 |  700 |
| 15 |  700 |
| 16 |  700 |
| 17 |  700 |
| 18 |  700 |
| 19 |  700 |
| 20 |  700 |
| 21 |  700 |
| 22 |  700 |
| 23 |  700 |
| 24 |  700 |
| 25 |  700 |
| 26 |  700 |
| 27 |  700 |
|  1 |  800 |
|  2 |  800 |
|  3 |  800 |
|  4 |  800 |
|  5 |  800 |
|  6 |  800 |
|  7 |  800 |
|  8 |  800 |
|  9 |  800 |
| 10 |  800 |
| 11 |  800 |
| 12 |  800 |
| 13 |  800 |
| 14 |  800 |
| 15 |  800 |
| 16 |  800 |
| 17 |  800 |
| 18 |  800 |
| 19 |  800 |
| 20 |  800 |
| 21 |  800 |
| 22 |  800 |
| 23 |  800 |
| 24 |  800 |
| 25 |  800 |
| 26 |  800 |
| 27 |  800 |
|  1 |  900 |
|  2 |  900 |
|  3 |  900 |
|  4 |  900 |
|  5 |  900 |
|  6 |  900 |
|  7 |  900 |
|  8 |  900 |
|  9 |  900 |
| 10 |  900 |
| 11 |  900 |
| 12 |  900 |
| 13 |  900 |
| 14 |  900 |
| 15 |  900 |
| 16 |  900 |
| 17 |  900 |
| 18 |  900 |
| 19 |  900 |
| 20 |  900 |
| 21 |  900 |
| 22 |  900 |
| 23 |  900 |
| 24 |  900 |
| 25 |  900 |
| 26 |  900 |
| 27 |  900 |
|  1 | 1000 |
|  2 | 1000 |
|  3 | 1000 |
|  4 | 1000 |
|  5 | 1000 |
|  6 | 1000 |
|  7 | 1000 |
|  8 | 1000 |
|  9 | 1000 |
| 10 | 1000 |
| 11 | 1000 |
| 12 | 1000 |
| 13 | 1000 |
| 14 | 1000 |
| 15 | 1000 |
| 16 | 1000 |
| 17 | 1000 |
| 18 | 1000 |
| 19 | 1000 |
| 20 | 1000 |
| 21 | 1000 |
| 22 | 1000 |
| 23 | 1000 |
| 24 | 1000 |
| 25 | 1000 |
| 26 | 1000 |
| 27 | 1000 |