Mysql
 sql >> Base de données >  >> RDS >> Mysql

MySql :Initialise la variable mySql dans une requête

Vous n'avez pas besoin d'initialiser la variable dans la sous-requête. Vous pouvez à la place initialiser la variable en utilisant CROSS JOIN :

SELECT time_of_day, 
       @channel_rank := IF(@current_channel = channel, 1, 
                             @channel_rank + 1) AS channel_rank, 
       @current_channel := channel AS channel,Views
FROM
(
   SELECT time_of_day,channel, SUM(Views) AS 'Views'
   FROM access_logs_meaningful_optimized
   WHERE `time_of_day` = 0
   AND playing_date = '2016-10-26' 
   GROUP BY channel
   ORDER BY SUM(views) DESC
   LIMIT 5
) AS xx
CROSS JOIN (SELECT @channel_rank := 0) var