【问题标题】:Executing a sql query multiple times in MySQL similar to GO in TSQL(SQL Server)MySQL中多次执行sql查询,类似于TSQL(SQL Server)中的GO
【发布时间】:2021-10-18 18:31:23
【问题描述】:

我试图在 MySQL 中多次运行以下命令。有没有像 TSQL/SQL Server for MySQL 中的 GO 命令这样简单的方法?

select
  now(),
  @@max_connections as "Max Connections",
  count(host) as "Current Connections"
from information_schema.processlist; 

想让演示保持简单。不是在寻找 SPROC。

【问题讨论】:

  • “多次”是什么意思?最简单的方法是编写存储过程。
  • 我想为演示运行上述命令 100 次,并显示通过命令行实用程序运行的查询。

标签: mysql


【解决方案1】:
mysql> set @query = 'select
    '>   now(),
    '>   @@max_connections as "Max Connections",
    '>   count(host) as "Current Connections"
    '> from information_schema.processlist; ';
Query OK, 0 rows affected (0.00 sec)

mysql> prepare stmt from @query;
Query OK, 0 rows affected (0.01 sec)
Statement prepared

mysql> execute stmt;
+---------------------+-----------------+---------------------+
| now()               | Max Connections | Current Connections |
+---------------------+-----------------+---------------------+
| 2021-08-16 18:22:03 |             512 |                   1 |
+---------------------+-----------------+---------------------+
1 row in set (0.01 sec)

然后您可以在同一个会话中任意多次execute stmt(准备好的语句具有当前会话的范围)。

更多信息请参见https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html

【讨论】:

  • 谢谢比尔。这很有帮助。如何在不参与的情况下循环运行执行 stmt 100 次?使用 SQL Server/TSQL,GO 命令可以让您轻松做到这一点。像 SELECT * FROM table GO 100;这将运行 SELECT 语句 100 次。
  • 我会用 Python 写一个脚本。
  • 根据您的操作系统,您可以编写一个带有循环的 shell 脚本,该循环重复执行类似于mysql <query.sql 的内容,其中query.sql 是一个包含 SQL 查询的文本文件。 stackoverflow.com/questions/8055694/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多