gpt4 book ai didi

mysql + 显示表并为每个表插入行号

转载 作者:行者123 更新时间:2023-11-29 11:29:59 25 4
gpt4 key购买 nike

此命令显示表格。

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 |
+----+------------------------------+
<小时/>

编辑1 - 这很接近

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)
<小时/>

编辑2 - 这就是我想要的

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)

最佳答案

您可以使用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%';

关于mysql + 显示表并为每个表插入行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37580450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com