mysql + show tables并为每个表插入一个行号

时间:2016-06-01 23:37:15

标签: mysql

此命令显示表格。

mysql> show tables like '%xyz%';
+------------------------------+
| Tables_in_XYZ (%xyz%) |
+------------------------------+
| xyz2                         |
| xyz23                        |
| xyz23_linuxLineEnding        |
+------------------------------+
3 rows in set (0.00 sec)

有什么命令可以在每个表中插入索引编号,例如

+----+------------------------------+
| id | Tables_in_XYZ (%xyz%)        |
+----+------------------------------+
|  1 | xyz2                         |
|  2 | xyz23                        |
|  3 | xyz23_linuxLineEnding        |
+----+------------------------------+

EDIT1 - 这很接近

mysql> select (@rn := @rn + 1) as id, table_name from information_schema.tables where table_name LIKE '%xyz%';
+------+-----------------------+
| id   | table_name            |
+------+-----------------------+
| NULL | xyz2                  |
| NULL | xyz23                 |
| NULL | xyz23_linuxLineEnding |
+------+-----------------------+
3 rows in set (0.00 sec)

EDIT2 - 这就是我想要的

mysql> set @rn :=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select (@rn := @rn + 1) as id, table_name from information_schema.tables where table_name LIKE '%xyz%';
+------+-----------------------+
| id   | table_name            |
+------+-----------------------+
|    1 | xyz2                  |
|    2 | xyz23                 |
|    3 | xyz23_linuxLineEnding |
+------+-----------------------+
3 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:1)

您可以使用information_schema.tables

select (@rn := @rn + 1) as id, table_name
from information_schema.tables cross join
     (select @rn := 0) params
where table_name like '%xyz%';