限制基于数据库值返回的结果
LIMIT RESULTS RETURNED BASED ON DB VALUE
我有两个 table,类别和故事。
故事 table 包含按类别组织的内容。
categories_id, category_name, category_story_count
1, news, 2
2, funnies, 3
stories_id, categories_id, story_name, story_content, story_active
1, 1, "Tax Hike", "blah blah", 1
2, 1, "Tax Cuts", "blah blah", 1
2, 1, "Election", "blah blah", 1
4, 2, "Peanuts", "blah blah", 1
5, 2, "Garfield", "blah blah", 1
6, 2, "Archie", "blah blah", 1
我想要一个查询,它将 return 基于 category_story_count 每个类别的正确故事数,如果故事是活跃的 (story_active = 1)
所以结果应该是这样的:
"news", "Tax Hike"
"news", "Tax Cuts"
"funnies", "Peanuts"
"funnies", "Garfield"
"funnies", "Archie"
两个“新闻”故事,因为新闻类别 1,有一个 category_story_count = 2,三个“搞笑”故事,因为搞笑 2,有一个 category_story_count = 3
我尝试过内部连接、嵌套和限制,但无法达到 return 我的目标。
如有任何帮助,我们将不胜感激。
编辑:
MySQL版本()
8.0.23
这是一个使用 window 函数的解决方案:
with cte as (
select *, row_number() over (partition by c.categories_id order by s.stories_id) as rownum
from Categories as c join Stories as s using (categories_id)
) select * from cte where rownum <= category_story_count;
+---------------+---------------+----------------------+------------+------------+---------------+--------------+--------+
| categories_id | category_name | category_story_count | stories_id | story_name | story_content | story_active | rownum |
+---------------+---------------+----------------------+------------+------------+---------------+--------------+--------+
| 1 | new | 2 | 1 | Tax Hike | blah blah | 1 | 1 |
| 1 | new | 2 | 2 | Tax Cuts | blah blah | 1 | 2 |
| 2 | funnies | 3 | 4 | Peanuts | blah blah | 1 | 1 |
| 2 | funnies | 3 | 5 | Garfield | blah blah | 1 | 2 |
| 2 | funnies | 3 | 6 | Archie | blah blah | 1 | 3 |
+---------------+---------------+----------------------+------------+------------+---------------+--------------+--------+
在 MySQL 8.0.23.
上测试
我有两个 table,类别和故事。
故事 table 包含按类别组织的内容。
categories_id, category_name, category_story_count
1, news, 2
2, funnies, 3
stories_id, categories_id, story_name, story_content, story_active
1, 1, "Tax Hike", "blah blah", 1
2, 1, "Tax Cuts", "blah blah", 1
2, 1, "Election", "blah blah", 1
4, 2, "Peanuts", "blah blah", 1
5, 2, "Garfield", "blah blah", 1
6, 2, "Archie", "blah blah", 1
我想要一个查询,它将 return 基于 category_story_count 每个类别的正确故事数,如果故事是活跃的 (story_active = 1)
所以结果应该是这样的:
"news", "Tax Hike"
"news", "Tax Cuts"
"funnies", "Peanuts"
"funnies", "Garfield"
"funnies", "Archie"
两个“新闻”故事,因为新闻类别 1,有一个 category_story_count = 2,三个“搞笑”故事,因为搞笑 2,有一个 category_story_count = 3
我尝试过内部连接、嵌套和限制,但无法达到 return 我的目标。
如有任何帮助,我们将不胜感激。
编辑: MySQL版本() 8.0.23
这是一个使用 window 函数的解决方案:
with cte as (
select *, row_number() over (partition by c.categories_id order by s.stories_id) as rownum
from Categories as c join Stories as s using (categories_id)
) select * from cte where rownum <= category_story_count;
+---------------+---------------+----------------------+------------+------------+---------------+--------------+--------+
| categories_id | category_name | category_story_count | stories_id | story_name | story_content | story_active | rownum |
+---------------+---------------+----------------------+------------+------------+---------------+--------------+--------+
| 1 | new | 2 | 1 | Tax Hike | blah blah | 1 | 1 |
| 1 | new | 2 | 2 | Tax Cuts | blah blah | 1 | 2 |
| 2 | funnies | 3 | 4 | Peanuts | blah blah | 1 | 1 |
| 2 | funnies | 3 | 5 | Garfield | blah blah | 1 | 2 |
| 2 | funnies | 3 | 6 | Archie | blah blah | 1 | 3 |
+---------------+---------------+----------------------+------------+------------+---------------+--------------+--------+
在 MySQL 8.0.23.
上测试