0

我有这个查询“SELECT Name, Type FROM tabel WHERE Name LIKE :key ORDER BY Type LIMIT 0,25”
我想要做的是从每种类型中获取 5 行并保持对 LIMIT 25 的总响应
我的表结构看起来有点像这样:
名称| A型
| 1
乙 | 2
C | 4D
| 2
E | 2
楼 | 4G
| 1
小时| 2
我 | 2
焦 | 2
所以在答案中我想得到
A 1
G 1
B 2
D 2
E 2
H 2
I 2
C 4
F 4

我尝试了很多东西,但我做不到。任何人都可以帮助我吗

4

2 回答 2

0

尝试在 order by 后使用 Group by type :

SELECT
  ...
FROM
  ...
WHERE
  ...
ORDER BY
 type
GROUP BY
  type;
于 2013-04-15T17:55:55.660 回答
0

我一直在尝试查询我在网上找到的与 PDO 一起解决我的问题的查询,并尝试了很多方案,直到这有效:

        $query = "set @num := 0, @Type := ''";
        $query2 = "SELECT Name, Type, (SELECT(@num := if(@Type = Type, @num + 1, 1))) as row_number, ".
        "(@Type := Type) as dummy FROM table WHERE Name LIKE ? ".
        "GROUP BY Type, Name, SearchBy HAVING row_number < 6"; 
    $stmt = $this->pdo->prepare($query);
    $stmt = $this->pdo->prepare($query2);
            $stmt->execute(array($name));
            $count = $stmt->rowCount();
            $rows = $stmt->fetchAll();

这会给我每种类型的 5 个我想要的

于 2013-04-18T19:26:54.437 回答