从“显示表格”中选择数据 MySQL查询

发布于 2024-07-04 12:59:28 字数 166 浏览 7 评论 0原文

是否可以从 MySQL 中的 show table 中进行选择?

SELECT * FROM (SHOW TABLES) AS `my_tables`

沿着这些思路,尽管上面的方法不起作用(至少在 5.0.51a 上)。

Is it possible to select from show tables in MySQL?

SELECT * FROM (SHOW TABLES) AS `my_tables`

Something along these lines, though the above does not work (on 5.0.51a, at least).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

青衫负雪 2024-07-11 12:59:28

你可能比你想象的更接近 - SHOW TABLES 已经表现出来了很像 SELECT 语句。 这是一个 PHP 示例,说明如何获取其“行”:

$pdo = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
foreach ($pdo->query("SHOW TABLES") as $row) {
    print "Table $row[Tables_in_$dbname]\n";
}

SHOW TABLES 的行为类似于单列表上的 SELECT。 该列名称是 Tables_in_ 加上数据库名称。

You may be closer than you think — SHOW TABLES already behaves a lot like a SELECT statement. Here's a PHP example of how you might fetch its "rows":

$pdo = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
foreach ($pdo->query("SHOW TABLES") as $row) {
    print "Table $row[Tables_in_$dbname]\n";
}

SHOW TABLES behaves like a SELECT on a one-column table. That column name is Tables_in_ plus the database name.

夜巴黎 2024-07-11 12:59:28

您不能像示例中那样将 SHOW 语句放入子查询中。 唯一可以进入子查询的语句是SELECT

正如其他答案所述,您可以直接使用 SELECT 查询 INFORMATION_SCHEMA,并通过这种方式获得更大的灵活性。

MySQL 的 SHOW 语句在内部只是针对 INFORMATION_SCHEMA 表的查询。

用户@physicalattraction 在大多数其他答案上发表了此评论:

这为您提供了有关表的(元)信息,而不是表的内容,正如OP预期的那样。 – 物理吸引力

相反,OP 的问题并没有说他们想要选择所有表中的数据。 他们说他们想从 SHOW TABLES 的结果中进行选择,这只是一个表名列表。

如果OP确实想从所有表中选择所有数据,那么答案是否定的,你不能通过一个查询来完成它。 每个查询必须显式命名其表。 您不能使表名成为变量或同一查询的另一部分的结果。 此外,给定查询结果的所有行必须具有相同的列。

因此,从所有表中选择所有数据的唯一方法是运行 SHOW TABLES,然后针对该结果中指定的每个表运行另一个查询。

You can't put SHOW statements inside a subquery like in your example. The only statement that can go in a subquery is SELECT.

As other answers have stated, you can query the INFORMATION_SCHEMA directly with SELECT and get a lot more flexibility that way.

MySQL's SHOW statements are internally just queries against the INFORMATION_SCHEMA tables.

User @physicalattraction has posted this comment on most other answers:

This gives you (meta)information about the tables, not the contents of the table, as the OP intended. – physicalattraction

On the contrary, the OP's question does not say that they want to select the data in all the tables. They say they want to select from the result of SHOW TABLES, which is just a list of table names.

If the OP does want to select all data from all tables, then the answer is no, you can't do it with one query. Each query must name its tables explicitly. You can't make a table name be a variable or the result of another part of the same query. Also, all rows of a given query result must have the same columns.

So the only way to select all data from all tables would be to run SHOW TABLES and then for each table named in that result, run another query.

丿*梦醉红颜 2024-07-11 12:59:28

您是否考虑过查询 INFORMATION_SCHEMA.Tables? 如在

SELECT ic.Table_Name,
    ic.Column_Name,
    ic.data_Type,
    IFNULL(Character_Maximum_Length,'') AS `Max`,
    ic.Numeric_precision as `Precision`,
    ic.numeric_scale as Scale,
    ic.Character_Maximum_Length as VarCharSize,
    ic.is_nullable as Nulls, 
    ic.ordinal_position as OrdinalPos, 
    ic.column_default as ColDefault, 
    ku.ordinal_position as PK,
    kcu.constraint_name,
    kcu.ordinal_position,
    tc.constraint_type
FROM INFORMATION_SCHEMA.COLUMNS ic
    left outer join INFORMATION_SCHEMA.key_column_usage ku
        on ku.table_name = ic.table_name
        and ku.column_name = ic.column_name
    left outer join information_schema.key_column_usage kcu
        on kcu.column_name = ic.column_name
        and kcu.table_name = ic.table_name
    left outer join information_schema.table_constraints tc
        on kcu.constraint_name = tc.constraint_name
order by ic.table_name, ic.ordinal_position;

Have you looked into querying INFORMATION_SCHEMA.Tables? As in

SELECT ic.Table_Name,
    ic.Column_Name,
    ic.data_Type,
    IFNULL(Character_Maximum_Length,'') AS `Max`,
    ic.Numeric_precision as `Precision`,
    ic.numeric_scale as Scale,
    ic.Character_Maximum_Length as VarCharSize,
    ic.is_nullable as Nulls, 
    ic.ordinal_position as OrdinalPos, 
    ic.column_default as ColDefault, 
    ku.ordinal_position as PK,
    kcu.constraint_name,
    kcu.ordinal_position,
    tc.constraint_type
FROM INFORMATION_SCHEMA.COLUMNS ic
    left outer join INFORMATION_SCHEMA.key_column_usage ku
        on ku.table_name = ic.table_name
        and ku.column_name = ic.column_name
    left outer join information_schema.key_column_usage kcu
        on kcu.column_name = ic.column_name
        and kcu.table_name = ic.table_name
    left outer join information_schema.table_constraints tc
        on kcu.constraint_name = tc.constraint_name
order by ic.table_name, ic.ordinal_position;
不回头走下去 2024-07-11 12:59:28
SELECT * FROM INFORMATION_SCHEMA.TABLES

这应该是一个好的开始。 有关更多信息,请查看INFORMATION_SCHEMA 表

SELECT * FROM INFORMATION_SCHEMA.TABLES

That should be a good start. For more, check INFORMATION_SCHEMA Tables.

千笙结 2024-07-11 12:59:28

我认为你想要的是 MySQL 的 information_schema 视图:
http://dev.mysql.com/doc/refman/ 5.0/en/tables-table.html

I think what you want is MySQL's information_schema view(s):
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html

初见终念 2024-07-11 12:59:28

您可以创建一个存储过程并将表名称放入游标中,然后循环遍历表名称以显示数据。

存储过程入门:
http://www.mysqltutorial.org/getting-started- with-mysql-stored-procedures.aspx

创建游标:
http://www.mysqltutorial.org/mysql-cursor/

例如,

CREATE PROCEDURE `ShowFromTables`()
BEGIN

DECLARE v_finished INTEGER DEFAULT 0;
DECLARE c_table varchar(100) DEFAULT "";

DECLARE table_cursor CURSOR FOR 
SELECT table_name FROM information_schema.tables WHERE table_name like 'wp_1%';

DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET v_finished = 1;

OPEN table_cursor;

get_data: LOOP

FETCH table_cursor INTO c_table;

IF v_finished = 1 THEN 
LEAVE get_data;
END IF;

SET @s=CONCAT("SELECT * FROM ",c_table,";");

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END LOOP get_data;

CLOSE table_cursor;

END

然后调用存储过程:

CALL ShowFromTables();

You can create a stored procedure and put the table names in a cursor, then loop through your table names to show the data.

Getting started with stored procedure:
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx

Creating a cursor:
http://www.mysqltutorial.org/mysql-cursor/

For example,

CREATE PROCEDURE `ShowFromTables`()
BEGIN

DECLARE v_finished INTEGER DEFAULT 0;
DECLARE c_table varchar(100) DEFAULT "";

DECLARE table_cursor CURSOR FOR 
SELECT table_name FROM information_schema.tables WHERE table_name like 'wp_1%';

DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET v_finished = 1;

OPEN table_cursor;

get_data: LOOP

FETCH table_cursor INTO c_table;

IF v_finished = 1 THEN 
LEAVE get_data;
END IF;

SET @s=CONCAT("SELECT * FROM ",c_table,";");

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END LOOP get_data;

CLOSE table_cursor;

END

Then call the stored procedure:

CALL ShowFromTables();
尬尬 2024-07-11 12:59:28

在MySql 5.1中你可以尝试

show tables like 'user%';

输出:

mysql> show tables like 'user%';

+----------------------------+

| Tables_in_test (user%) |

+----------------------------+

| user                       |

| user_password              |

+----------------------------+

2 rows in set (0.00 sec)

in MySql 5.1 you can try

show tables like 'user%';

output:

mysql> show tables like 'user%';

+----------------------------+

| Tables_in_test (user%) |

+----------------------------+

| user                       |

| user_password              |

+----------------------------+

2 rows in set (0.00 sec)
缘字诀 2024-07-11 12:59:28

是的,SELECT from table_schema 对于系统管理非常有用。 如果您有很多服务器、数据库、表...有时您需要删除或更新一堆元素。 例如,要创建 DROP 所有前缀名称为“wp_old_...”的表的查询:

SELECT concat('DROP TABLE ', table_name, ';') FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = '*name_of_your_database*'
AND table_name LIKE 'wp_old_%';

Yes, SELECT from table_schema could be very usefull for system administration. If you have lot of servers, databases, tables... sometimes you need to DROP or UPDATE bunch of elements. For example to create query for DROP all tables with prefix name "wp_old_...":

SELECT concat('DROP TABLE ', table_name, ';') FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = '*name_of_your_database*'
AND table_name LIKE 'wp_old_%';
毁梦 2024-07-11 12:59:28
SELECT column_comment FROM information_schema.columns WHERE table_name = 'myTable' AND column_name = 'myColumnName'

这将返回以下评论:myTable.myColumnName

SELECT column_comment FROM information_schema.columns WHERE table_name = 'myTable' AND column_name = 'myColumnName'

This will return the comment on: myTable.myColumnName

郁金香雨 2024-07-11 12:59:28

我不明白您为什么要使用 SELECT * FROM 作为语句的一部分。

12.5.5.30。 显示表语法

I don't understand why you want to use SELECT * FROM as part of the statement.

12.5.5.30. SHOW TABLES Syntax

初见 2024-07-11 12:59:28

我想你想要SELECT * FROM INFORMATION_SCHEMA.TABLES

参见http://dev.mysql.com/doc/refman/5.0/en/tables-table.html

I think you want SELECT * FROM INFORMATION_SCHEMA.TABLES

See http://dev.mysql.com/doc/refman/5.0/en/tables-table.html

不必你懂 2024-07-11 12:59:28

据我所知,除非您从 INFORMATION_SCHEMA 中进行选择,正如其他人提到的那样。

然而,SHOW 命令非常灵活,
例如:

SHOW tables like '%s%'

Not that I know of, unless you select from INFORMATION_SCHEMA, as others have mentioned.

However, the SHOW command is pretty flexible,
E.g.:

SHOW tables like '%s%'
最美的太阳 2024-07-11 12:59:28

计数:

SELECT COUNT(*) as total FROM (SELECT TABLE_NAME as tab, TABLES.* FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='database_name' GROUP BY tab) tables;

列出:

SELECT TABLE_NAME as table, TABLES.* FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='database_name' GROUP BY table;

To count:

SELECT COUNT(*) as total FROM (SELECT TABLE_NAME as tab, TABLES.* FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='database_name' GROUP BY tab) tables;

To list:

SELECT TABLE_NAME as table, TABLES.* FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='database_name' GROUP BY table;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文