简体   繁体   English

如何在MySQL中通过一次选择为每个“类别”获得5条记录?

[英]How can I get 5 records for each 'category' with one select in MySQL?

I have table topics : 我有餐桌topics

ID title body ctg_topic_id ... 
1  aaa   bbb  1            ...
2  aa2   bb2  2            ...
3  aa3   bb3  1            ...
4  aa4   bb4  3            ...
5  aa5   bb5  3            ...
6  aa6   bb6  1            ...
.......
105 a105 b105 23       ...

How can I get 5 records for each ctg_topic_id by 1 select? 通过选择1如何获得每个ctg_topic_id的5条记录?

This SQL script for mysql will give you the first five of all cats. 这个用于mysql的SQL脚本将为您提供所有猫的前五名。

SELECT * FROM your_table WHERE ctg_topic_id IN (SELECT DISTINCT ctg_topic_id from your_table) GROUP BY ctg_topc_id LIMIT 0,5

Via PHP. 通过PHP。 (i omit the PHP connection code) (我省略了PHP连接代码)

First, get the list; 首先,获取列表;

$stm = $pdo->preprare("SELECT DISTINCT cth_topc_id FROM my_table");
$stm->execute():
$cats = $stm->fetchAll();
$result = array();
foreach ($cats as $cat) {
$query = $pdo->prepare("SELECT * my_table WHERE cth_topic_id=? LIMIT 0,5");
$query->bindParams(1,$cat);
$pdo->execute();
$result[$i] = $query->fetchAll();
}

This simplified code should produce a matrix containing N arrays (where N is the number of distinct cats) that contains an array of 5 elements of the result set. 此简化的代码应生成一个包含N个数组的矩阵(其中N是不同的猫的数量),该矩阵包含结果集的5个元素的数组。

I've not checked the code so any hints will be corrected thanks. 我没有检查代码,所以任何提示都会得到纠正,谢谢。

One problem is that LIMIT 0,5 will get the first 5 items, withot any logic. 一个问题是,LIMIT 0,5将获得前5个项目,而没有任何逻辑。 If you want to sort use ORDER BY for example by ('date') etc. 如果要排序,请使用ORDER BY,例如按('date')等。

SELECT ctg_topic_id from something, GROUP BY ctg_topic_id....

它将选择所有类别。

You can not. 你不能。 You can do it with one query using UNION or read this How to select the first/least/max row per group in SQL 您可以使用UNION进行一次查询,也可以阅读此方法如何在SQL中选择每个组的第一行/最小行/最大行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为MySQL中的每个ID只选择一行? - How can I select only one row for each ID in MySQL? 如何在WordPress查询中获取分类法类别的所有记录? - How can I get all records of taxonomy category in WordPress Query? 如何仅选择每个类别的一条记录? - How to select only one record of each category? CakePHP:如何使用“漂亮”网址选择一个类别的所有元素? (/类别/ NAME_OF_CATEGORY) - CakePHP: How can I select all elements of one category with a “nice” url? (/category/NAME_OF_CATEGORY) 如何从类别ID数组中选择mysql中的记录? - How to select records in mysql based from array of category id? 如何使用php从mysql数据库中选择相似的记录? - How can I select similar records from mysql database with php? MySQL选择所有记录,并为每个选择单独的记录-在一个查询中 - MySQL selecting all records and for each select separate record - in one query 如何在MySQL中选择所有类别并计算每个类别文章的数量 - How to select all categories and count number of each category articles in MySQL 我如何才能获取过去7天中只有记录的那几天的MySQL记录,并每天返回记录呢? - How can I get MySQL records from past 7 days, but only days that have a record, and return each day as a record? 我怎样才能创建一个数据库每年和 select 我想要一个 select 的年份并显示那一年的记录? - How can I create a database each year and select the year I want for a select and display the records for that year?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM