投稿内に@country_rank
は@image_rank
である必要があります。また、user variables
の初期化はCROSS JOIN
に追加されています。
ランダムN選択を得るには、ORDER BY rand()
はimage_rank
世代の前でなければなりません。ここで
SELECT category_child, `img_url`, image_rank
FROM
(SELECT img_url,
@image_rank := IF(@current_category = category_child, @image_rank + 1, 1) AS image_rank,
@current_category := category_child AS category_child
FROM (SELECT * FROM images ORDER BY category_child, rand()) AS images_rand CROSS JOIN
(SELECT @image_rank := 0, @current_category := NULL) param
) ranked
WHERE image_rank <= 5;
はデモです:
SQL:
-- data
create table images(category_child int, img_url char(20));
insert into images values
(1,'d'),(1,'c'),(1,'a'),(1,'b'),(1,'x'),(1,'s'),
(2,'d'),(2,'c'),(2,'a'),(2,'b'),(2,'x'),(2,'s');
select * from images;
-- Query wanted
SELECT category_child, `img_url`, image_rank
FROM
(SELECT img_url,
@image_rank := IF(@current_category = category_child, @image_rank + 1, 1) AS image_rank,
@current_category := category_child AS category_child
FROM (SELECT * FROM images ORDER BY category_child, rand()) AS images_rand CROSS JOIN
(SELECT @image_rank := 0, @current_category := NULL) param
) ranked
WHERE image_rank <= 5;
出力:
mysql> select * from images;
+----------------+---------+
| category_child | img_url |
+----------------+---------+
| 1 | d |
| 1 | c |
| 1 | a |
| 1 | b |
| 1 | x |
| 1 | s |
| 2 | d |
| 2 | c |
| 2 | a |
| 2 | b |
| 2 | x |
| 2 | s |
+----------------+---------+
12 rows in set (0.00 sec)
mysql> -- Query wanted
mysql> SELECT category_child, `img_url`, image_rank
-> FROM
-> (SELECT img_url,
-> @image_rank := IF(@current_category = category_child, @image_rank + 1, 1) AS image_rank,
-> @current_category := category_child AS category_child
-> FROM (SELECT * FROM images ORDER BY category_child, rand()) AS images_rand CROSS JOIN
-> (SELECT @image_rank := 0, @current_category := NULL) param
-> ) ranked
-> WHERE image_rank <= 5;
+----------------+---------+------------+
| category_child | img_url | image_rank |
+----------------+---------+------------+
| 1 | s | 1 |
| 1 | x | 2 |
| 1 | b | 3 |
| 1 | c | 4 |
| 1 | d | 5 |
| 2 | b | 1 |
| 2 | s | 2 |
| 2 | x | 3 |
| 2 | c | 4 |
| 2 | a | 5 |
+----------------+---------+------------+
10 rows in set (0.00 sec)
-- Run again and generate different rand selection
mysql> SELECT category_child, `img_url`, image_rank
-> FROM
-> (SELECT img_url,
-> @image_rank := IF(@current_category = category_child, @image_rank + 1, 1) AS image_rank,
-> @current_category := category_child AS category_child
-> FROM (SELECT * FROM images ORDER BY category_child, rand()) AS images_rand CROSS JOIN
-> (SELECT @image_rank := 0, @current_category := NULL) param
-> ) ranked
-> WHERE image_rank <= 5;
+----------------+---------+------------+
| category_child | img_url | image_rank |
+----------------+---------+------------+
| 1 | x | 1 |
| 1 | c | 2 |
| 1 | d | 3 |
| 1 | a | 4 |
| 1 | s | 5 |
| 2 | a | 1 |
| 2 | c | 2 |
| 2 | s | 3 |
| 2 | d | 4 |
| 2 | x | 5 |
+----------------+---------+------------+
10 rows in set (0.00 sec)
あなたの投稿では、country_rankはimage_rank - typoだったはずです。まだすべての結果のランクは、CROSS JOIN ... paramを追加した後の戻り値です。これはmysqlバージョンと関係がありますか? mysqlバージョン – usearch
で編集しましたが、私は副選択に変更しました – usearch