如何使用 JSON_EXTRACT 处理不存在的值?

How to deal with not existing values using JSON_EXTRACT?

我有一个对象列表。每个对象包含几个属性。现在我想做一个 SELECT 语句,给我一个 属性 值的列表。简化列表如下所示:

[
    [
        {
            "day": "2021-10-01",
            "entries": [
                {
                    "name": "Start of competition",
                    "startTimeDelta": "08:30:00"
                }
            ]
        },
        {
            "day": "2021-10-02",
            "entries": [
                {
                    "name": "Start of competition",
                    "startTimeDelta": "03:30:00"
                }
            ]
        },
        {
            "day": "2021-10-03",
            "entries": [
                {
                    "name": "Start of competition"
                }
            ]
        }
    ]
]

工作SELECT现在

SELECT
    JSON_EXTRACT(column, '$.days[*].entries[0].startTimeDelta') AS list
FROM table

返回结果为

[
    "08:30:00",
    "03:30:00"
]

但我想得到的(也是期待的)是

[
    "08:30:00",
    "03:30:00",
    null
]

我该怎么做或如何更改 SELECT 语句以便我也能在列表中获得 NULL 值?

SELECT startTimeDelta
FROM test
CROSS JOIN JSON_TABLE(val,
                      '$[*][*].entries[*]' COLUMNS (startTimeDelta TIME PATH '$.startTimeDelta')) jsontable

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=491f0f978d200a8a8522e3200509460e


Do you also have a working idea for MySQL< 8? – Lars

What is max amount of objects in the array on the 2nd level? – Akina

Well it's usually less than 10 – Lars

SELECT JSON_EXTRACT(val, CONCAT('$[0][', num, '].entries[0].startTimeDelta')) startTimeDelta
FROM test
-- up to 4 - increase if needed
CROSS JOIN (SELECT 0 num UNION SELECT 1 UNION SELECT 2 UNION SELECT 3) nums
WHERE JSON_EXTRACT(val, CONCAT('$[0][', num, '].entries[0]')) IS NOT NULL;

https://www.db-fiddle.com/f/xnCCSTGQXevcpfPH1GAbUo/0