我能够将所有表名及其相应的列显示为:
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进行分组,但是它不起作用,请问如何显示以上内容? 如何一次显示表名并显示各个表的所有列名?
答案 0 :(得分:2)
您要使用的是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
;