Jika group_concat tidak mengembalikan nilai, adakah terdapat sebarang penyelesaian?
P粉212971745
P粉212971745 2023-08-30 19:44:24
0
2
601
<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>
P粉212971745
P粉212971745

membalas semua(2)
P粉043295337

Saya agak keliru mengapa anda mahu menggunakan SQL dinamik. Fungsi tetingkap nampaknya melakukan apa yang anda mahu:

select t.*
from (select t.*,
             max(case when question = 'Age' then answer end) over (partition by respondent_id ) as age,
             max(case when question = 'height' then answer end) over (partition by respondent_id ) as height
      from source_table st
     ) t
where demographic is null;

Jika anda tidak tahu lajur "disemak", saya rasa anda boleh menggunakan ini sebagai templat.

P粉823268006

@FaNo_FN @ysth

Saya berjaya membetulkannya menggunakan biola yang anda siarkan dan ulasan anda.

Saya menambah kod berikut antara dua pertanyaan untuk menyemak sama ada pembolehubah ditetapkan.

SET @sql := IF(@sql IS NULL,'',@sql);

Saya masih GROUP_CONCAT之前添加了第二个CONCAT() menambah pemisah ','.

Berikut adalah pautan ke fiddle.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan