Skip to content

Instantly share code, notes, and snippets.

@Alextrapixel
Created September 12, 2025 13:27
Show Gist options
  • Save Alextrapixel/a176814c57de0ee05857777711efe551 to your computer and use it in GitHub Desktop.
Save Alextrapixel/a176814c57de0ee05857777711efe551 to your computer and use it in GitHub Desktop.
Show column as array in mysql

If your table has:

name small big
apple 10 20
banana 5 15

We can make this query:

SELECT 
    name,
    JSON_ARRAY(
        JSON_OBJECT('size', 'small', 'quantity', small),
        JSON_OBJECT('size', 'big', 'quantity', big)
    ) AS detail
FROM your_table;

And the query will return:

name detail
apple [{"size": "small", "quantity": 10}, {"size": "big", "quantity": 20}]
banana [{"size": "small", "quantity": 5}, {"size": "big", "quantity": 15}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment