更新时间:2022-11-27 19:42:16
您根本无法拥有返回可变列数的静态SQL语句.每次不同地区的数量发生变化时,您都需要构建这样的语句.为此,您首先执行
You simply cannot have a static SQL statement returning a variable number of columns. You need to build such statement each time the number of different districts changes. To do that, you execute first a
SELECT DISTINCT District FROM district_details;
这将为您提供包含详细信息的地区列表.然后,您构建一条遍历先前结果(伪代码)的SQL语句
This will give you the list of districts where there are details. You then build a SQL statement iterating over the previous result (pseudocode)
statement = "SELECT name "
For each row returned in d = SELECT DISTINCT District FROM district_details
statement = statement & ", SUM(IF(District=""" & d.District & """,1 ,0)) AS """ & d.District & """"
statement = statement & " FROM district_details GROUP BY name;"
并执行该查询.然后,您需要在代码中处理可变数量的列
And execute that query. You'll then need have to handle in your code the processing of the variable number of columns