わかりました、私は答えを見つけました。
元のコード:
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)
@ヒント
@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;