简体   繁体   English

如何使用mysql列出视图中使用的表

[英]How to list tables used in a view with mysql

I need a list of tables being used in a view in mysql.我需要在 mysql 的视图中使用的表列表。 For example, if I have a view like:例如,如果我有这样的观点:

SELECT * FROM table1
JOIN  table2
ON table1.id = table2.id

I want to get: table1,table2我想得到:table1,table2

Unfortunately, I don't believe that's possible directly.不幸的是,我认为这不可能直接实现。 Instead, you need to query and parse the actual view definition:相反,您需要查询和解析实际的视图定义:

SELECT VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE
TABLE_NAME = ?;

. .

mysql> CREATE VIEW vw_test AS  
    -> SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS
    -> WHERE TABLE_NAME = 'vw_test';
+------------------------------------------------------------------+
| VIEW_DEFINITION                                                  |
+------------------------------------------------------------------+
| select * from table1 join table2 on table1.id = table2.id;       |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

then you could use any of the following tools to parse the table names:那么您可以使用以下任何工具来解析表名:

  1. Terence Parr's ANTLR parser generator (Java, but can generate parsers in any one of a number of target languages) has several SQL grammars available, including a couple for PL/SQL, one for a SQL Server SELECT statement, one for mySQL, and one for ISO SQL - ( http://www.antlr.org/grammar/list ). Terence Parr 的 ANTLR 解析器生成器(Java,但可以用多种目标语言中的任何一种生成解析器)有几种可用的 SQL 语法,包括一对用于 PL/SQL、一种用于 SQL Server SELECT 语句、一种用于 mySQL 以及一种对于 ISO SQL - ( http://www.antlr.org/grammar/list )。

    I took this from SO answer here: SQL parser library for Java - Retrieve the list of table names present in a SQL statement我从这里的 SO 答案中获取了这个: Java 的 SQL 解析器库 - 检索 SQL 语句中存在的表名列表

  2. Data Tools Project - SQL Development Tools ( http://www.eclipse.org/datatools/project_sqldevtools/ ).数据工具项目 - SQL 开发工具( http://www.eclipse.org/datatools/project_sqldevtools/ )。

    Here's the documentation for the SQL Query Parser ( http://www.eclipse.org/datatools/project_sqldevtools/sqltools_doc/SQL%20Query%20Parser%20User%20documentation.htm ).这是SQL 查询解析器的文档 ( http://www.eclipse.org/datatools/project_sqldevtools/sqltools_doc/SQL%20Query%20Parser%20User%20documentation.htm )。

  3. Here's a blog with descriptions of how to " Get columns and tables in SQL script (Java version) " http://www.dpriver.com/blog/list-of-demos-illustrate-how-to-use-general-sql-parser/get-columns-and-tables-in-sql-script/这是一个博客,描述了如何“ 在 SQL 脚本(Java 版本)中获取列和表http://www.dpriver.com/blog/list-of-demos-illustrate-how-to-use-general-sql -parser/get-columns-and-tables-in-sql-script/
  4. Or write yourself a custom mySQL proc based on the following (found here - http://www.sqlparser.com/fetch-table-column-name-example-extact-all-table-field-name.php ):或者根据以下内容为自己编写一个自定义的 mySQL proc(在这里找到 - http://www.sqlparser.com/fetch-table-column-name-example-extact-all-table-field-name.php ):

     SELECT c_mandant, hist_datum, parkey1, parkey2, funktionscode, ma_parkey, me_parkey , CASE WHEN EXISTS (SELECT 1 FROM CDS_H_GRUPPE GRP1 WHERE GRP1.c_mandant = c_mandant AND GRP1.hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) AND GRP1.funktionscode = 'H' AND GRP1.parkey1 = ma_parkey) THEN 1 ELSE NULL END MA_ME , CASE WHEN EXISTS (SELECT 1 FROM CDS_H_GRUPPE GRP2 WHERE GRP2.c_mandant = c_mandant AND GRP2.hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) AND GRP2.funktionscode = 'U' AND GRP2.parkey1 = me_parkey) THEN 1 ELSE NULL END ME_MA , ROW_NUMBER() OVER (PARTITION BY c_mandant, ma_parkey, me_parkey ORDER BY c_mandant, ma_parkey, me_parkey) ANZ_MA FROM (SELECT c_mandant, hist_datum, parkey1, parkey2, funktionscode , CASE WHEN funktionscode = 'U' THEN parkey1 ELSE parkey2 END MA_PARKEY , CASE WHEN funktionscode = 'U' THEN NULL ELSE parkey1 END ME_PARKEY FROM CDS_H_GRUPPE WHERE funktionscode IN ('U', 'H') AND hist_datum = ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) )

this is what you want... this can get table used in Views and table which joined together.. but it can get one join... if want more add few more hint..这就是你想要的......这可以在视图中使用表和连接在一起的表......但它可以得到一个连接......如果想要更多添加更多提示......

hope this would solve your question...希望这能解决你的问题......

select 
    case 
        when view_definition regexp '.*from +.*'
        then substring_index(substring_index(view_definition, 'from ', -1), ' ', 1)
    end as 'primary table',
    case 
        when view_definition regexp '.*join +.*'
        then substring_index(substring_index(view_definition, 'join ', -1), ' ', 1)
    end as 'joined table'
from information_schema.views where table_name="YOUR VIEW NAME" and table_schema="shotbot_production";

No that is not possible.,那是不可能的。 You have to look for the definition of the view and get that done by yourself manually.您必须查找视图的定义并自己手动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM