MySQL,存储过程,如何从两个查询中获得结果,更新解决

时间:2012-12-15 00:53:02

标签: mysql stored-procedures count rowset

我的程序:(这是一个计划)

  delimiter //
  CREATE PROCEDURE get_random_data()
  BEGIN
  Declare Count_a INT;
      SELECT Floor(Count(`id_x`) * Rand()) INTO Count_a From `x_data`  Where `ok_w` = 1 And `active` = 2 And `time` >= Unix_timestamp();

   # Now We select Some Rows
   SELECT `id_x` From `x_data` Where `spec` = 1 Limit Count_a,3;

   # And if Count of this select is small than < 3

   IF(row_count() < 3) then
      SELECT `id_anonsu` From `x_data` Where `spec` = 0 Limit Count_a,2;
   END IF ;


  END//
  delimiter ;

//目标

如何从BOTH查询中获取结果? //来自if语句之前的查询

//查询语句中的第二个,如果第一个查询计数小于3

2 个答案:

答案 0 :(得分:1)

这取决于您使用的客户端界面。例如,JDBC有statement.getMoreResults()

有趣的是,查询同时包含分隔符//和//注释。

答案 1 :(得分:1)

好的,我找到了答案。

原始代码:

   IF(row_count() < 3) then
       SELECT `id_anonsu` From `x_data` Where `spec` = 0 Limit Count_a,2;
   END IF ;

替换为:

IF(FOUND_ROWS() < 3) THEN SELECT `id_anonsu` From `x_data` Where `spec` = 0 Limit Count_a,2;
END IF ;

更好的版本:

查询工作正常(谁使用LIMIT) 我们需要知道存储过程中的(查询)返回行数

带有节目计数的版本

    DROP PROCEDURE IF EXISTS test;
    Delimiter //
    CREATE PROCEDURE test(OUT w INT)
    BEGIN
    Declare Counter_a INT DEFAULT 0;


        Set @id_counter = 0;

        SELECT Floor(Count(`id_x`) * Rand()) INTO Counter_a From `x_data`  Where `ok_w` = 1 And `active` = 2 And `time` >= Unix_timestamp();

        SELECT @id_counter := @id_counter + 1, `id_x` From `x_data` Where `spec` = 1 Limit Counter_a,3;


        IF @id_counter < 3 THEN 
            Set Counter_a = Counter_a + 1;
            SELECT `id_x` From `x_data` Limit Counter_a,2;


        END IF;

        Set w = @id_counter;


    END //
    Delimiter ;

输出如下:

  mysql> Call test(@z); Select @z;
  +--------------------------------+-----------+
  | @id_counter := @id_counter + 1 |   id_x    |
  +--------------------------------+-----------+
  |                              1 |         7 |
  |                              2 |         8 |
  +--------------------------------+-----------+
  2 rows in set (0.00 sec)

  +-----------+
  |   id_x    |
  +-----------+
  |         8 |
  +-----------+
  1 row in set (0.01 sec)

  Query OK, 0 rows affected (0.01 sec)

  +------+
  | @z   |
  +------+
  |    2 |
  +------+
  1 row in set (0.00 sec)


隐藏计数的版本

    DROP PROCEDURE IF EXISTS test;
    Delimiter //
    CREATE PROCEDURE test(OUT w INT)
    BEGIN
    Declare Counter_a INT DEFAULT 0;


        Set @id_counter = 0;

        SELECT Floor(Count(`id_x`) * Rand()) INTO Counter_a From `x_data`  Where `ok_w` = 1 And `active` = 2 And `time` >= Unix_timestamp();

        SELECT `id_x` From `x_data` Where @id_counter := @id_counter + 1 And `spec` = 1 Limit Counter_a,3;


        IF @id_counter < 3 THEN 
            Set Counter_a = Counter_a + 1;
            SELECT `id_x` From `x_data` Limit Counter_a,2;


        END IF;

        Set w = @id_counter;


    END //
    Delimiter ;

输出如下:

  mysql> Call test(@z); Select @z;
  +-----------+
  |   id_x    |
  +-----------+
  |         7 |
  |         8 |
  +-----------+
  2 rows in set (0.00 sec)

  +-----------+
  |   id_x    |
  +-----------+
  |         8 |
  +-----------+
  1 row in set (0.01 sec)

  Query OK, 0 rows affected (0.01 sec)

  +------+
  | @z   |
  +------+
  |    2 |
  +------+
  1 row in set (0.00 sec)

@TIP

 @id_counter := @id_counter + 1

必须在所有条件之前。

例如:

[.....] Where @id_counter := @id_counter + 1 And `field_2` > 1 LIMIT 0, 5;

将永远返回1 ...


正确版本

[.....] Where `field_2` > 1 And  [Other conditions] And @id_counter := @id_counter + 1  LIMIT 0, 5;