sql >> Database teknologi >  >> RDS >> Mysql

MySql :Initialiser mySql-variabelen i en forespørgsel

Du behøver ikke at initialisere variablen i underforespørgslen. Du kan i stedet initialisere variablen ved at bruge 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



  1. Hvordan matcher man poster, der er knyttet til et bestemt sæt af andre poster?

  2. Medbring din egen sky tilgængelig for DigitalOcean

  3. Mysql søg efter streng og tal ved hjælp af MATCH() AGAINST()

  4. PHP MySQL sæt Connection Timeout