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.

Revisions

  1. Alextrapixel created this gist Sep 12, 2025.
    22 changes: 22 additions & 0 deletions mysql_show_as_array.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    If your table has:
    | name | small | big |
    |:---|:---:|:---:|
    | apple | 10 | 20|
    | banana | 5 | 15 |

    We can make this query:
    ```sql
    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}]` |