[英]mysql show databases by condition
I have databases named like this: 我有这样命名的数据库:
database_100
database_120
database_153
database_143
...
I'll call the numbers at the end 'Databse ID' for the sake of this example. 为了这个示例,我将在“ Databse ID”的末尾调用这些数字。
Doing something like SHOW DATABASES LIKE "database_%
will output all the DBs and it's fine. Now I want to show databases with id = 100 or 120 or 320
this is just an example, the list of IDs itself is dynamically pulled from an array. 做类似
SHOW DATABASES LIKE "database_%
将输出所有DB,这很好。现在我想显示id = 100 or 120 or 320
数据库,这只是一个例子,ID列表本身是从数组中动态提取的。
How can I do something like that? 我该怎么做?
EDIT: 编辑:
I'm trying to use INFORMATION_SCHEMA 我正在尝试使用INFORMATION_SCHEMA
USE INFORMATION_SCHEMA;
SELECT `SCHEMA_NAME` from `SCHEMATA` WHERE `SCHEMA_NAME` LIKE "database_%";
but still can't figure out how to select by the dynamic IDs. 但仍然不知道如何通过动态ID进行选择。
I'm thinking about something like (in pseudo code): 我在想类似的东西(用伪代码):
USE INFORMATION_SCHEMA;
SELECT `SCHEMA_NAME` from `SCHEMATA` WHERE `SCHEMA_NAME` LIKE "database_$var" WHILE $var in (121, 153, 143);
Could it be done only in SQL ? 只能在SQL中完成吗?
you can use prepare statement ,Try the below code it will resolve your issue... However you need modify this line set @schema := 'tes%';
您可以使用prepare语句,尝试下面的代码将解决您的问题...但是,您需要修改此行
set @schema := 'tes%';
according to your requirement 根据您的要求
mysql> USE INFORMATION_SCHEMA
Database changed
mysql> prepare stmt from
-> 'SELECT SCHEMA_NAME from SCHEMATA WHERE SCHEMA_NAME like ?';
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> set @schema := 'tes%';
Query OK, 0 rows affected (0.00 sec)
mysql> execute stmt using @schema;
+-------------+
| SCHEMA_NAME |
+-------------+
| test |
+-------------+
1 row in set (0.00 sec)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.