python - 如何显示一次表名并显示各个表的所有列名?
问题描述
我能够将所有表名及其各自的列显示为:
SNO Tables in Database Column names
1 table1 a
2 table1 b
3 table2 c
4 table2 d
哪个html文件是:
<html>
<head><link rel="stylesheet" href="{{ url_for('static', filename='css/index.css') }}"></head>
<body>
<div>
<table border="1" align="center" class="w3-table w3-striped">
<caption><strong>Farm Automation Details</strong></caption>
<thead>
<tr>
<th>SNO</th>
<th style="text-align:center">Tables in Database</th>
<th style="text-align:center">Column names</th>
</tr>
</thead>
<tbody>
{%for row in result%}
<tr>
<td></td>
<td style="text-align:center">{{ row[0] }}</td>
<td style="text-align:center">{{ row[1] }} </td>
</tr>
{%endfor%}
</table>
</div>
</body>
</html>
并获取我写的表和列名:
sql="select table_name,column_name from information_schema.columns where table_schema = 'farmautomation' order by table_name,ordinal_position"
cursor.execute(sql)
result = cursor.fetchall()
我期望将表格显示为:
SNO Tables in Database Column names
1 table1 a,b
2 table2 c,d
我尝试在 table_name 上进行分组,但没有成功,请问我该如何显示如上?如何显示一次表名并显示各个表的所有列名?
解决方案
您要使用的是 GROUP_CONCAT 函数:
select table_name, group_concat(column_name order by column_name asc) as column_names
from information_schema.columns
where table_schema = 'farmautomation'
group by table_name
;
推荐阅读
- javascript - 将基本 /docs/ 路径更改为 /home/ | 剑龙
- error-handling - 找到运算符“**”的“0”定义,无法确定 VHDL 中“**”的精确重载匹配定义
- layout - Laravel 急流惯性持久化布局
- c# - 如何以编程方式在 Windows 上启用位置
- django - Django 开发阶段最佳实践
- powerbi - 如何根据 power bi measure/dax 中的另一列列出列的值
- java - 当依赖项具有 spring-cloud-starter-sleuth 时生菜抛出 NPE
- javascript - 是否可以防止 prisma 中每个查询的新连接数?
- sas - 对 SAS 非常陌生
- autodesk-forge - 根据内容自动调整 Forge 文本标记框大小