A
A
Alex Wells2018-10-06 03:58:41
MySQL
Alex Wells, 2018-10-06 03:58:41

Get max() from sub-query results?

There is a request with two , but a third is needed -select something as some_field

select max(some_field_1, some_field_2) as max_of_some_fields
- but this, logically, does not work, since the search is trying to find such fields in the database. I would gladly send the whole request, but it is very large (4026 characters), so here is my attempt in a nutshell:
SELECT *,
       (SELECT Coalesce(Max(won_amount), 0)
        FROM   `fast_games`
               INNER JOIN `fast_bets` AS `winning_fast_bets`
                       ON `winning_fast_bets`.`id` =
                          `fast_games`.`winning_bet_id`
        WHERE  users.id = winning_fast_bets.user_id
               AND `fast_games`.`state` = ?)
       AS `max_fast_jackpot`,
       (SELECT Coalesce(Max(won_amount), 0)
        FROM   `coinflip_games`
               INNER JOIN `coinflip_bets` AS `winning_coinflip_bets`
                       ON `winning_coinflip_bets`.`id` =
                          `coinflip_games`.`winning_bet_id`
        WHERE  users.id = winning_coinflip_bets.user_id
               AND `coinflip_games`.`state` = ?)
       AS `max_coinflip_jackpot`,
       (SELECT Max(max_fast_jackpot, max_coinflip_jackpot))
       AS
       `max_jackpot`
FROM   `users`
WHERE  `id` = ?

max_jackpot - required field. How to make it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ponaehal, 2018-10-08
@ponaehal

More or less like this:

SELECT Max(max_fast_jackpot, max_coinflip_jackpot) 
FROM (
SELECT *,
       (SELECT Coalesce(Max(won_amount), 0)
        FROM   `fast_games`
               INNER JOIN `fast_bets` AS `winning_fast_bets`
                       ON `winning_fast_bets`.`id` =
                          `fast_games`.`winning_bet_id`
        WHERE  users.id = winning_fast_bets.user_id
               AND `fast_games`.`state` = ?)
       AS `max_fast_jackpot`,
       (SELECT Coalesce(Max(won_amount), 0)
        FROM   `coinflip_games`
               INNER JOIN `coinflip_bets` AS `winning_coinflip_bets`
                       ON `winning_coinflip_bets`.`id` =
                          `coinflip_games`.`winning_bet_id`
        WHERE  users.id = winning_coinflip_bets.user_id
               AND `coinflip_games`.`state` = ?)
       AS `max_coinflip_jackpot`  )

FROM   `users`
WHERE  `id` = ?
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question