【发布时间】:2019-11-02 09:39:34
【问题描述】:
我正在使用 postgreSQL 查询。例如,我想从 SQL 查询中提取所有信息
sql = " select d_year, s_nation, p_category, sum(lo_revenue - lo_supplycost) as profit from DATES, CUSTOMER, SUPPLIER, PART, LINEORDER where lo_custkey = c_custkey and lo_suppkey = s_suppkey and lo_partkey = p_partkey and lo_orderdate = d_datekey and c_region = 'AFRICA' and s_region = 'AFRICA' and (d_year = 1996 or d_year = 1997) and (p_mfgr = 'MFGR#2' or p_mfgr = 'MFGR#4') group by d_year, s_nation, p_category order by d_year, s_nation, p_category "
我想获取所有相关表、所有选择谓词和所有连接谓词、按部分分组和按部分排序。
我使用了sqlparse,我找到了一种只获取相关表格的方法。
是否有任何示例说明如何提取此信息?
【问题讨论】:
-
您可以使用 Antlr 解析 SQL 语句并提取 AST。
标签: python sql postgresql text-extraction sql-parser