如何在MariaDB中按顺序返回行(包括空值)

qfe3c7zg  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(141)

如何在MariaDB中按顺序返回包含空值的行。
我有一个ID列表,我想查询,但实际结果不是我所期望的。
记录05.173不存在,但我需要空行按顺序排列。
这可能吗?

SELECT b.*, a.sia_id
    FROM (
        SELECT '02.352' AS sia_id
        UNION ALL
        SELECT '05.173' AS sia_id
        UNION ALL
        SELECT '02.352' AS sia_id
        UNION ALL
        SELECT '05.173' AS sia_id
        UNION ALL
        SELECT '05.451' AS sia_id
    ) AS a
    LEFT JOIN estoques AS b
        USING (sia_id) order by `validade` asc

当前结果

[
{null},
{null},
{siaId:'02.352', ...},
{siaId:'02.352', ...},
{siaId:'05.451', ...}
]

预期结果

[
{siaId:'02.352', ...},
{null},
{siaId:'02.352', ...},
{null},
{siaId:'05.451', ...},
]
up9lanfz

up9lanfz1#

您可以将排序属性添加到派生表:

SELECT b.*, a.sia_id
FROM (
    SELECT '02.352' AS si_id, 1 as oa
    UNION ALL
    SELECT '05.173' AS si_id, 2
    UNION ALL
    SELECT '02.352' AS si_id, 3
    UNION ALL
    SELECT '05.173' AS sia_id, 4
    UNION ALL
    SELECT '05.451' AS sia_id, 5
) AS a
LEFT JOIN estoques AS b
    USING (sia_id) 
order by a.oa

结果集中没有明确排序依据的任何顺序都应视为重合。

相关问题