Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 将多个行项目合并为一个行项目_Mysql_Sql - Fatal编程技术网

Mysql 将多个行项目合并为一个行项目

Mysql 将多个行项目合并为一个行项目,mysql,sql,Mysql,Sql,我需要一个SQL查询,它可以将一个表中的多个行项目组合成一个行项目。 我有两张桌子: Table: results || Table: conditions --------------- || ---------------------------------- | id | value | || | id | result_id | name | value | --------------- || ---

我需要一个SQL查询,它可以将一个表中的多个行项目组合成一个行项目。 我有两张桌子:

    Table: results     ||    Table: conditions
    ---------------    ||    ----------------------------------
    | id  | value |    ||    | id  | result_id | name | value |
    ---------------    ||    ----------------------------------
    |  1  | 5.2   |    ||    |  1  |       1   | temp |   5   |
    |  2  | 9.9   |    ||    |  2  |       1   | wave | 100   |
    |  3  | 12.1  |    ||    |  3  |       2   | temp |   5   |
    |  4  | 6.6   |    ||    |  4  |       2   | wave | 200   |
    |     |       |    ||    |  5  |       3   | temp |  25   |
    |     |       |    ||    |  6  |       3   | wave | 100   |
    |     |       |    ||    |  7  |       4   | temp |  25   |
    |     |       |    ||    |  8  |       4   | wave | 200   |
    ---------------    ||    ----------------------------------
条件表中的条件与所示的条件相比有很多不同,但在本例中,我只关心waveand temp

我想做一个查询,让我选择某些结果/条件,结果如下:

-----------------------------------
| result_id | temp | wave | value |
-----------------------------------
|       1   |   5  | 100  |   5.2 |
|       4   |  25  | 200  |   6.6 |
-----------------------------------
或根据条件温度=5

-----------------------------------
| result_id | temp | wave | value |
-----------------------------------
|       1   |   5  | 100  |   5.2 |
|       2   |   5  | 200  |   9.9 |
-----------------------------------
到目前为止,我已使用以下方法成功筛选出一种情况:

选择conditions.result\u id、conditions.name、conditions.value、results.value 从条件 results.id=conditions.result\u id上的内部联接结果 其中results.id=1或results.id=4 和条件。名称如temp; -其中conditions.name如temp和conditions.value=5
但我不知道如何搜索与相同结果id匹配的wave并将其放在同一行。

您可以尝试使用条件聚合函数。使用max函数时的情况

问题1:

:


重新研究mysql pivot和mysql条件聚合以及mysql准备好的语句。如果我想筛选temp=5而不是按id筛选呢?看起来它只是强制id 1和4的temp为5。我在问如何搜索temp为5的ID,并以聚合形式显示它们,类似于我编辑的问题的最后一个表格。
SELECT results.id, 
       MAX(case when conditions.name = 'temp' then conditions.value end)temp, 
       MAX(case when conditions.name = 'wave' then conditions.value end)wave, 
       results.value
FROM conditions
INNER JOIN results ON results.id = conditions.result_id
WHERE (results.id = 1 OR results.id = 4)
GROUP BY results.id,results.value
| id | temp | wave | value |
|----|------|------|-------|
|  1 |    5 |  100 |   5.2 |
|  4 |   25 |  200 |   6.6 |