2017-02-06 7 views
0

どうすればこの問題をmysqlで達成できますか?複数のクエリを1つのテーブルに結合するmysql

結果テーブル

+ ----------------- + --------------------- - + ------ +
| st-id |用語|スコア|
+ ----------------- + ----------------------- + ----- - +
| 20001 | 1 | 5 |
| 20002 | 1 | 6 |
| 20001 | 2 | 6 |
| 20002 | 2 | 4 |
| 20003 | 1 | 7 |
| 20003 | 2 | 9 |

+ ----------------- + ----------------------- + --- --- + -

結果のクエリは、この

ようにする必要があります+ ------------------------- ------- +
| st-id |スコア->用語1 |スコア - >用語2
+ ------------------------------- +
| 20001 | 5                     |             |
| 20002 | 6                   |             |
| 20003 | 7                   |             |

+ ------------------------------- +

(select st-id from result-table where term=1) union (select st-id from result-table where term=2) 

を試みたが、結果を追加します。

+0

条件付き集計を探します – Mihai

答えて

0

あなたは(常に一致している場合、行が常に内部一致しない場合は左)に参加

select a.st-id, a.score as `score->term 1`, b.score as `score->term 2` 
    from `result-table` as a 
    left join `result-table` as a on a.`st-id` = b.`st-id` 
    where a.term=1 
    and b.term=2 

または

select a.`st-id`, a.score as `score->term 1`, b.score as `score->term 2` 
    from `result-table` as a 
    inner join `result-table a`s a on a.`st-id` = b.`st-id` 
    where a.term=1 
    and b.term=2 

を使用する必要がありますし、あなたはST-IDとして名を使用しないでください( - はmysqlのオペラです。)実際にbackticsを使用する必要がある場合

関連する問題