You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLite如何查询realRevenue列之后的所有列?

查询指定列之后的所有列

要实现查询realRevenue列之后的所有列,由于手动罗列列名效率极低,需要借助数据库的系统元数据视图动态获取目标列名,再拼接成最终查询语句。不同数据库的实现细节略有差异,以下是主流数据库的解决方案:

MySQL

利用information_schema.columns视图获取列的定义顺序,筛选出realRevenue之后的列并拼接成列名列表:

-- 第一步:获取目标列名
SELECT GROUP_CONCAT(column_name SEPARATOR ', ')
FROM information_schema.columns
WHERE table_schema = DATABASE()  -- 自动匹配当前数据库
  AND table_name = 'urls'
  AND ordinal_position > (
      SELECT ordinal_position
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'urls'
        AND column_name = 'realRevenue'
  )
ORDER BY ordinal_position;

将查询得到的列名列表复制到SELECT语句中即可,示例:

SELECT realRevenue, clicksGermany, clicksUSA, clicksIndia FROM urls;

PostgreSQL

同样基于information_schema.columns视图(注意PostgreSQL默认将列名转为小写,若创建表时显式使用大写列名,需用双引号包裹):

-- 获取目标列名
SELECT string_agg(column_name, ', ')
FROM information_schema.columns
WHERE table_schema = current_schema()
  AND table_name = 'urls'
  AND ordinal_position > (
      SELECT ordinal_position
      FROM information_schema.columns
      WHERE table_schema = current_schema()
        AND table_name = 'urls'
        AND column_name = 'realRevenue'  -- 大写列名需写为"realRevenue"
  )
ORDER BY ordinal_position;

SQL Server

使用sys.columns系统视图获取列的顺序信息:

-- 获取目标列名
SELECT STRING_AGG(name, ', ') WITHIN GROUP (ORDER BY column_id)
FROM sys.columns
WHERE object_id = OBJECT_ID('urls')
  AND column_id > (
      SELECT column_id
      FROM sys.columns
      WHERE object_id = OBJECT_ID('urls')
        AND name = 'realRevenue'
  );

注意事项

  • 这种方法完全依赖数据库记录的列定义顺序,如果后续在realRevenue和目标列之间新增列,需要重新执行查询更新列列表。
  • 若要实现自动适配表结构变更的动态查询,可以结合存储过程或动态SQL完成。

内容的提问来源于stack exchange,提问作者user20668417

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:01:27