【问题标题】:How to display 2 different data (Two Condition) in one table in mysql如何在mysql的一张表中显示2个不同的数据(两个条件)
【发布时间】:2018-06-30 08:14:06
【问题描述】:

我在 php mysql 中显示不同的数据时遇到了问题。我想在两种情况下显示不同的数据。例如:

我有桌子:

tbl_hrresponse

| id_reponse | id_atribut | skala | step |
| 1          | 1          | 1     | 1    |
| 1          | 2          | 4     | 1    |
| 1          | 3          | 2     | 1    |
| 1          | 1          | 5     | 2    |
| 1          | 2          | 6     | 2    |
| 1          | 3          | 2     | 2    |

所以,我想这样输出:

| id_reponse | id_atribut | skala step 1 | skala step 2 |
| 1          | 1          | 1            | 5      |
| 1          | 2          | 4            | 6      |
| 1          | 3          | 2            | 2      |

我有这样的代码:

$k = $db->pdo->prepare("select *, (CASE WHEN tbl_hresponder.step = '1' THEN tbl_hresponder.skala END) AS k,
                                                  (CASE WHEN tbl_hresponder.step = '2' THEN tbl_hresponder.skala END) AS q
                                                  from tbl_hresponder, tbl_atribut
                                                  where tbl_hresponder.id_atribut = tbl_atribut.id_atribut
                                                  AND tbl_hresponder.id_responder = '".$_GET['report']."'
                                                  AND tbl_hresponder.id_fservqual = '".$rfs['id_fservqual']."'
                                                  AND tbl_hresponder.step = 1");

【问题讨论】:

    标签: php mysql sql pdo


    【解决方案1】:

    你可以尝试使用CASE WHENmax函数。

    SELECT t1.id_reponse,
           t1.id_atribut,
           max(case when t1.step = 1 then t1.skala end) `skala step 1`, 
           max(case when t1.step = 2 then t1.skala end) `skala step 2`
    FROM tbl_hresponse t1 
    GROUP  BY  
           t1.id_reponse,
           t1.id_atribut
    

    sqlfiddle:http://sqlfiddle.com/#!9/92a73e/4

    结果

    | id_reponse | id_atribut | skala step 1 | skala step 2 |
    |------------|------------|--------------|--------------|
    |          1 |          1 |            1 |            5 |
    |          1 |          2 |            4 |            6 |
    |          1 |          3 |            2 |            2 |
    

    【讨论】:

      猜你喜欢
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      相关资源
      最近更新 更多