itemと評価テーブルが2つあり、itemと評価の平均をまとめて、かつ、UserIDはitemテーブルの値を利用したいのですが、うまくいきません。
SQLをどのように記載すべきか教えてください。
MAX(userID)がいけないのですが、Selectで平均値した値をleftjoinできるのでしょうか?
SQL
1select `itemID`, MAX(Name), AVG(value) as r_avg from `table_a` left join `table_b` on `table_a`.`itemID` = `table_b`.`itemID` left join `table_c` on `table_a`.`UserId` = `table_c`.`UserId` group by `itemID`;
●table_a:itemテーブル
| itemID | Item | UserId |
|---|---|---|
| 1 | item1 | 1 |
| 2 | item2 | 2 |
●table_b:item評価テーブル
| itemID | value | UserID |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 5 | 2 |
| 1 | 3 | 3 |
●table_c:usersテーブル
| UserID | Name |
|---|---|
| 1 | userA |
| 2 | userB |
| 3 | userC |
●期待するクエリ回答
item1,userA,3
item2,userB,null
回答1件
あなたの回答
tips
プレビュー