在mysql中创建过程以创建每个选择查询

时间:2016-09-27 08:04:54

标签: mysql stored-procedures

我有这样的查询:

SELECT
    a.CREATED, 
    a.FIRST_REVISION, 
    a.SECOND_REVISION , 
    a.THIRD_REVISION , 
    a.FOURTH_REVISION, 
    a.FIFTH_REVISION
FROM tb_master_repair_estimate a
WHERE a.REPAIR_ESTIMATE_ID = 91

我得到了这样的结果:

+---------------------+----------------+-----------------+----------------+-----------------+----------------+
| CREATED             | FIRST_REVISION | SECOND_REVISION | THIRD_REVISION | FOURTH_REVISION | FIFTH_REVISION |
+---------------------+----------------+-----------------+----------------+-----------------+----------------+
| 2016-09-26 04:32:22 | 2016-09-25     | 2016-09-25      | 2016-09-25     | NULL            | NULL           |
+---------------------+----------------+-----------------+----------------+-----------------+----------------+
1 row in set (0.00 sec)

我可以用这样的逻辑创建程序吗?

I will check in field FIRST_REVISION,
   if null, I used data CREATED to another query
   break;
I will check IN field SECOND_REVISION
   if null, I use data FIRST_REVISION to another query
   break
I will check IN field THIRD_REVISION
   if null, I use data SECOND_REVISION to another query
   break

我在想,有可能像程序一样管理它吗?

请举个例子,我是新手,在mysql中创建一个函数或过程。

CREATE FUNCTION F_CHECK_LAST_REVISED
     RETURNS DATE

BEGIN
    /*  LIKE THIS ONE */

END;

我创建了一个这样的程序:

DROP PROCEDURE IF EXISTS P_CHECK_LAST_REVISED; 

DELIMITER //

CREATE PROCEDURE P_CHECK_LAST_REVISED(id_cari int(10))

BEGIN

DECLARE pre varchar(50);
DECLARE one varchar(50);
DECLARE two varchar(50);
DECLARE three varchar(50);
DECLARE four varchar(50);
DECLARE five varchar(50);
DECLARE last_revision varchar(50);

SELECT CREATED, FIRST_REVISION, SECOND_REVISION, THIRD_REVISION, FOURTH_REVISION, FIFTH_REVISION        
    INTO one, two,three, four, five
    FROM tb_master_repair_estimate a
WHERE a.REPAIR_ESTIMATE_ID = id_cari;

IF one IS NULL THEN
    SELECT b.* FROM tb_repair_detail b
    WHERE b.REPAIR_ESTIMATE_ID = id_cari;

ELSEIF two IS NULL THEN
    SELECT c.* FROM tb_repair_detail_first_revision c
    WHERE c.REPAIR_ESTIMATE_ID = id_cari;

ELSEIF three IS NULL THEN
    SELECT d.* FROM tb_repair_detail_second_revision d
    WHERE d.REPAIR_ESTIMATE_ID = id_cari;   

ELSEIF four IS NULL THEN
    SELECT e.* FROM tb_repair_detail_third_revision e
    WHERE e.REPAIR_ESTIMATE_ID = id_cari;   

ELSEIF five IS NULL THEN
    SELECT f.* FROM tb_repair_detail_fourth_revision f
    WHERE f.REPAIR_ESTIMATE_ID = id_cari;   

ELSE
    SELECT g.* FROM tb_repair_detail_fifth_revision g
    WHERE gx.REPAIR_ESTIMATE_ID = id_cari;  

END IF;

END;
//

所以,call P_CHECK_LAST_REVISED(92),我觉得错误就是这样:

The used select statment hav a different number columns

1 个答案:

答案 0 :(得分:-1)

你可以试试这个

  

`DELIMITER //

     

创建程序测试(p_REPAIR_ESTIMATE_ID int)

     

开始

     

选择

     

a.CREATED,

     

a.FIRST_REVISION,

     

a.SECOND_REVISION,

     

a.THIRD_REVISION,

     

a.FOURTH_REVISION,

     

a.FIFTH_REVISION   来自tb_master_repair_estimate a

     

WHERE.REPAIR_ESTIMATE_ID = p_REPAIR_ESTIMATE_ID

     

结束;`