Mysql查询,每一行只获取4条记录

时间:2022-12-10 13:13:29

I am facing issue in one mysql query. The database records are as follows.

我在一个mysql查询中面临问题。数据库记录如下。

id  name
1    a1
2    a1
3    a1
4    a1
5    a1
6    a1
7    a1
8    a1

and my expected result is as follows

我的预期结果如下。

id          name
1,2,3,4      a1
5,6,7,8      a1

4 records in each row

每行记录4条

Can anyone please help me ?

谁能帮帮我吗?

1 个解决方案

#1


3  

Please try with this

请用这个

SELECT GROUP_CONCAT(id) FROM (SELECT
    CEIL(@rownum :=@rownum + 1 / 4) AS pageNo,
    id,
    name
FROM
    ( SELECT @rownum := 0) r, temp )  tempTable GROUP BY pageNo

#1


3  

Please try with this

请用这个

SELECT GROUP_CONCAT(id) FROM (SELECT
    CEIL(@rownum :=@rownum + 1 / 4) AS pageNo,
    id,
    name
FROM
    ( SELECT @rownum := 0) r, temp )  tempTable GROUP BY pageNo