If group_concat does not return a value, is there any solution?
P粉212971745
2023-08-30 19:44:24
<p>我写了一个查询,根据条件将几行转换为列。</p>
<p>然而,有些情况下没有行满足条件,但我希望返回一些结果。</p>
<p>下面是表格的示例和我要寻找的结果。</p>
<p>源表:</p>
<table class="s-table">
<thead>
<tr>
<th>id</th>
<th>respondent_id</th>
<th>人口统计</th>
<th>问题</th>
<th>回答</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>已检查</td>
<td>年龄</td>
<td>30</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>空</td>
<td>教育</td>
<td>硕士</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>已检查</td>
<td>身高</td>
<td>1.8m</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>空</td>
<td>收入</td>
<td>$1</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>空</td>
<td>地址</td>
<td>国际空间站</td>
</tr>
<tr>
<td>6</td>
<td>1</td>
<td>空</td>
<td>才艺</td>
<td>跳舞</td>
</tr>
<tr>
<td>7</td>
<td>2</td>
<td>已检查</td>
<td>年龄</td>
<td>20</td>
</tr>
<tr>
<td>8</td>
<td>2</td>
<td>空</td>
<td>教育</td>
<td>高中</td>
</tr>
<tr>
<td>9</td>
<td>2</td>
<td>已检查</td>
<td>身高</td>
<td>4m</td>
</tr>
<tr>
<td>10</td>
<td>2</td>
<td>空</td>
<td>收入</td>
<td>$3.2</td>
</tr>
<tr>
<td>11</td>
<td>2</td>
<td>空</td>
<td>地址</td>
<td>高海</td>
</tr>
<tr>
<td>12</td>
<td>2</td>
<td>空</td>
<td>才艺</td>
<td>唱歌</td>
</tr>
</tbody>
</table>
<p>转换后的示例结果:</p>
<table class="s-table">
<thead>
<tr>
<th>id</th>
<th>respondent_id</th>
<th>年龄</th>
<th>身高</th>
<th>问题</th>
<th>答案</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>1</td>
<td>30</td>
<td>1.8m</td>
<td>教育</td>
<td>硕士</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>30</td>
<td>1.8m</td>
<td>收入</td>
<td>$1</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>30</td>
<td>1.8m</td>
<td>地址</td>
<td>国际空间站</td>
</tr>
<tr>
<td>6</td>
<td>1</td>
<td>30</td>
<td>1.8m</td>
<td>才艺</td>
<td>跳舞</td>
</tr>
<tr>
<td>8</td>
<td>2</td>
<td>20</td>
<td>4m</td>
<td>教育</td>
<td>高中</td>
</tr>
<tr>
<td>10</td>
<td>2</td>
<td>20</td>
<td>4m</td>
<td>收入</td>
<td>$3.2</td>
</tr>
<tr>
<td>11</td>
<td>2</td>
<td>20</td>
<td>4m</td>
<td>地址</td>
<td>高海</td>
</tr>
<tr>
<td>12</td>
<td>2</td>
<td>20</td>
<td>4m</td>
<td>才艺</td>
<td>唱歌</td>
</tr>
</tbody>
</table>
<p>当前的MySQL语句:</p>
<pre class="brush:php;toolbar:false;">SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'(SELECT l.answer FROM source_table l where l.respondent_id = a.respondent_id AND l.question = ', b.question,')
AS ',b.question
)
) INTO @sql
FROM source_table b
WHERE b.demographic IS NOT NULL;
SET @sql =
CONCAT('SELECT respondents_id,
',@sql,',
a.question , a.answer
FROM source_table a
WHERE a.demographic IS NULL
GROUP BY id
');
PREPARE stmt FROM @sql;
EXECUTE stmt;</pre>
<p>为了澄清,上述查询在有“checked”的人口统计列时有效,但是当没有“checked”单元格时,整个查询失败。</p>
<p>因此,我希望有一个查询在所有情况下都能工作,无论是否有人口统计行。</p>
<p>如果没有人口统计行,则查询应该返回没有新列的数据。</p>
I'm a little confused why you want to use dynamic SQL. Window functions seem to do what you want:
If you don't know the "checked" column, I think you can use this as a template.
@FaNo_FN @ysth
I successfully fixed it using the fiddle you posted and your comment.
I added the following code between the two queries to check if the variable is set.
SET @sql := IF(@sql IS NULL,'',@sql);
I also added a second
CONCAT()
beforeGROUP_CONCAT
to add a ',' separator.This is the link to fiddle.