X
X
xonar2019-02-19 07:53:54
SQL
xonar, 2019-02-19 07:53:54

How to group data by field in JOIN?

Hello.
I combine 2 tables using JOIN and I need to group the price field using GROUP BY by the game_id field (game number), but it doesn’t work out.
Here is a piece of the request, where I stopped at the moment and a screen that I receive.

SELECT * FROM `roulette_bets` LEFT OUTER JOIN `roulette_games` ON `roulette_bets`.`game_id` = `roulette_games`.`id` WHERE `roulette_bets`.`user_id`=36454  GROUP BY roulette_bets.id  
ORDER BY `roulette_bets`.`game_id`  DESC

5c6b8a219fe22132175988.png
If I write GROUP BY roulette_bets.game_id in the query, then the following error occurs
: "#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'gosens.roulette_bets.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"
I tried it like this.
SELECT roulette_bets.game_id, roulette_bets.user_id, SUM(roulette_bets.price) as pricebets  FROM `roulette_bets` JOIN `roulette_games` ON `roulette_bets`.`game_id` = `roulette_games`.`id` WHERE `roulette_bets`.`user_id`=36454  GROUP BY roulette_bets.game_id
ORDER BY `roulette_bets`.`game_id` DESC

That's all right, it groups by game_id and successfully adds up the price field, but! It displays only 3 fields, and I need all the fields from two tables, but it displays as in the screenshot.
5c6b8bcf80b62854101893.png
Help, I'm confused.
The structure of the roulette_bets table is below: The structure of the roulette_games
5c6b91b28d9a3644193628.png
table is below: roulette_bets.game_id = roulette_games.id The PRICE price must be added to one sum by game_id And we get the game 64677 = , user_id=36454, price=5.2, etc.
5c6b91d093d5a797195277.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-02-19
@xonar

Have you tried reading and understanding the error message? After all, it is written in English and white, what is the problem with the grouping.
https://dev.mysql.com/doc/refman/5.7/en/group-by-h...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question