I need to get the count of records and need to display the result by duplicating in all of the records in the return result.
{
"id": 1,
"count1": "5"
}
As the json response currently I received the above output. But I need it is as follows.
{
"id": 1,
"count1": "5"
},
{
"id": 2,
"count1": "5"
},
{
"id": 3,
"count1": "5"
}
My attempted query as below.
$query = 'SELECT c.id, count(c) count1 FROM App\Entity\Car c';
How can I achieve my expected result?
If you have a numbers table, you can do:
SELECT c.id, n.n, count1
FROM (SELECT c.id, count(c) as count1
FROM `App\Entity\Car` c
GROUP BY c.id
) c JOIN
numbers n
ON n.n <= c.count1;
If you don't you can generate one on the fly if your table is big enough:
SELECT c.id, n.n, count1
FROM (SELECT c.id, count(c) as count1
FROM `App\Entity\Car` c
GROUP BY c.id
) c JOIN
(SELECT (@rn := @rn + 1) as n
FROM numbers n CROSS JOIN
(SELECT @rn := 0) params
) n
ON n.n <= c.count1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.