찾다

 >  Q&A  >  본문

group_concat이 값을 반환하지 않으면 해결책이 있습니까?

<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粉212971745P粉212971745471일 전598

모든 응답(2)나는 대답할 것이다

  • P粉043295337

    P粉0432953372023-08-31 18:54:03

    동적 SQL을 사용하려는 이유가 조금 혼란스럽습니다. 창 기능이 원하는 대로 작동하는 것 같습니다:

    으아악

    '체크됨' 열을 모르신다면 템플릿으로 사용해도 될 것 같아요.

    회신하다
    0
  • P粉823268006

    P粉8232680062023-08-31 09:57:01

    @FaNo_FN @ysth

    귀하가 게시한 바이올린과 귀하의 의견을 사용하여 성공적으로 수정했습니다.

    변수가 설정되었는지 확인하기 위해 두 쿼리 사이에 다음 코드를 추가했습니다.

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

    아직 GROUP_CONCAT之前添加了第二个CONCAT() ',' 구분 기호를 추가하는 중입니다.

    여기에 fiddle 링크가 있습니다.

    회신하다
    0
  • 취소회신하다