为什么 SQLite 无法在视图中同时执行 WHERE 和 ORDER BY 子句?

问题描述 投票:0回答:1

背景

我正在尝试在 SQLite 中复制来自 MySQL 的

INFORMATION_SCHEMA
视图(我有需要这些视图的工具)。

对于

INFORMATION_SCHEMA.COLUMNS
视图,我有以下查询

WITH RECURSIVE table_list AS 
(
    SELECT name, schema
    FROM pragma_table_list()
    UNION
    SELECT name, 'main'
    FROM pragma_module_list()
    WHERE name NOT LIKE 'fts%'
      AND name NOT LIKE 'rtree%'
),
table_info AS 
(
    SELECT
        'def' AS TABLE_CATALOG,
        tl.schema AS TABLE_SCHEMA,
        tl.name AS TABLE_NAME,
        ti.name AS COLUMN_NAME,
        ti.cid AS ORDINAL_POSITION,
        ti.dflt_value AS COLUMN_DEFAULT,
        IIF(ti."notnull" AND ti.pk, 'YES', 'NO') AS IS_NULLABLE,
        CASE UPPER(ti.type)
            WHEN 'TEXT' THEN 'varchar'
            WHEN 'INT' THEN 'bigint'
            WHEN 'REAL' THEN 'float'
            WHEN 'BLOB' THEN 'blob'
            WHEN 'INTEGER' THEN 'bigint'
            WHEN 'TINYINT' THEN 'bigint'
            WHEN 'SMALLINT' THEN 'bigint'
            WHEN 'MEDIUMINT' THEN 'bigint'
            WHEN 'BIGINT' THEN 'bigint'
            WHEN 'UNSIGNED BIG INT' THEN 'bigint'
            WHEN 'INT2' THEN 'bigint'
            WHEN 'INT8' THEN 'bigint'
            WHEN 'VARCHAR' THEN 'varchar'
            WHEN 'VARCHAR(255)' THEN 'varchar'
            ELSE 'varchar'
        END AS DATA_TYPE,
        65535 AS CHARACTER_MAXIMUM_LENGTH,
        65535 AS CHARACTER_OCTET_LENGTH,
        NULL AS NUMERIC_PRECISION,
        NULL AS NUMERIC_SCALE,
        NULL AS DATETIME_PRECISION,
        'utf8mb3' AS CHARACTER_SET_NAME,
        'BINARY' AS COLLATION_NAME,
        CASE UPPER(ti.type)
            WHEN 'TEXT' THEN 'varchar(65535)'
            WHEN 'INT' THEN 'int'
            WHEN 'REAL' THEN 'double'
            WHEN 'BLOB' THEN 'blob'
            WHEN 'INTEGER' THEN 'int'
            WHEN 'TINYINT' THEN 'int'
            WHEN 'SMALLINT' THEN 'int'
            WHEN 'MEDIUMINT' THEN 'int'
            WHEN 'BIGINT' THEN 'int'
            WHEN 'UNSIGNED BIG INT' THEN 'int'
            WHEN 'INT2' THEN 'int'
            WHEN 'INT8' THEN 'int'
            WHEN 'VARCHAR' THEN 'varchar(65535)'
            WHEN 'VARCHAR(255)' THEN 'varchar(65535)'
            ELSE 'varchar(65535)'
        END AS COLUMN_TYPE,
        IIF(ti.pk, 'PRI', '') AS COLUMN_KEY,
        IIF(tl.schema='information_schema', 'select', 'select,insert,update,references') AS PRIVILEGES,
        '' AS COLUMN_COMMENT,
        '' AS GENERATION_EXPRESSION,
        NULL AS SRS_ID,
        '' AS EXTRA
    FROM
        table_list tl,
        pragma_table_info(tl.name) ti
)
SELECT * 
FROM table_info;

返回所有表的所有列。

问题

对于这个查询,我可以添加一个

WHERE
子句:

...
SELECT * 
FROM table_info
WHERE TABLE_SCHEMA = 'main';

并且有效。

我还可以添加一个

ORDER BY
子句:

...
SELECT * 
FROM table_info
ORDER BY TABLE_NAME, ORDINAL_POSITION;

而且它也有效。

但是,当我将

WHERE
子句与
ORDER BY
子句组合时,查询不会返回任何行:

...
SELECT * 
FROM table_info 
WHERE TABLE_SCHEMA = 'main' 
ORDER BY TABLE_NAME, ORDINAL_POSITION;

我单独运行每个子查询,它们返回行。我还尝试仅使用

WHERE
子句或仅使用
ORDER BY
子句运行查询,并且它返回行。

因此,我预计将

WHERE
子句和
ORDER BY
子句结合起来会起作用 - 但事实并非如此。

SQLite版本:3.45.1

sql database sqlite view information-schema
1个回答
0
投票

将 SQLite 升级到 3.46.0 解决了我的问题。

我使用的是 mattn/go-sqlite3,目前版本为 3.45.1

© www.soinside.com 2019 - 2024. All rights reserved.