A
A
Albert Ushakov2020-06-05 12:39:40
PHP
Albert Ushakov, 2020-06-05 12:39:40

How to use GROUP BY to store the ID of the records that were grouped?

Here is an example of code that I am trying to implement, but it does not work.

SELECT *, 
id_category AS (
    SELECT id FROM bot_shop_category 
         WHERE name = a.name
    ) 
    FROM bot_shop_category a 
GROUP BY name

It is necessary to display the data in the array like this, but I don’t understand what the error is and how can this be done with one request?
0 => [ name : "Название сгруппированых категорий",  id_category :  {1,5,97,22, ...} ]
1 => [ name : "Название сгруппированых категорий 2",  id_category :  {13,52,971,224, ...} ]
... и.т.д

Tell me how to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-06-05
@FanatPHP

either group in php, or group_concat in the request, but then you still have to do explode on receipt
, plus you need to remember that by default the size of the value in group_concat is 1024 bytes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question