2017-02-03 18 views
-3

以下のmysql質問は最初の10行だけを返します。どのようにして10%に制限できますか?MySQLの結果の合計数に対するクエリ結果をパーセントで制限する方法は?

SELECT page, 
     poso, 
     diff 
FROM (SELECT page, 
       Count(*) AS poso, 
       (Sum(Date(timestamp) = Curdate()) - Sum( 
       Date(timestamp) = Date_sub(Curdate(), 
            INTERVAL 1 day))) 
         diff 
     FROM `behaviour` 
     WHERE Date(timestamp) >= Date_sub(Curdate(), INTERVAL 1 day) 
     GROUP BY page 
     ORDER BY (Sum(Date(timestamp) = Curdate()) - Sum( 
        Date(timestamp) = Date_sub(Curdate(), 
           INTERVAL 1 day)) 
       ) DESC 
     LIMIT 10) AS u 
ORDER BY diff DESC 
+6

の可能性のある重複した[MySQLの:?レコードの量の割合によってLIMIT](http://stackoverflow.com/questions/5615172/mysql-limit-of-the-of-of-of-of-of-record-of-records) –

答えて

-1

the answer to the duplicate questionから適応:ここ

SELECT page, 
     poso, 
     diff 
FROM (
    SELECT *, 
      @counter := @counter + 1 AS counter 
    FROM (select @counter:=0) AS initvar, 
      (SELECT page, 
        Count(*) AS poso, 
        (Sum(Date(timestamp) = Curdate()) - Sum( 
        Date(timestamp) = Date_sub(Curdate(), 
             INTERVAL 1 day))) 
          diff 
      FROM `behaviour` 
      WHERE Date(timestamp) >= Date_sub(Curdate(), INTERVAL 1 day) 
      GROUP BY page 
      ORDER BY (Sum(Date(timestamp) = Curdate()) - Sum( 
         Date(timestamp) = Date_sub(Curdate(), 
            INTERVAL 1 day)) 
        ) DESC) AS u 
) AS v 
WHERE counter <= 10/100 * @counter 
ORDER BY diff DESC; 

デモ:http://rextester.com/JKMBZR62923

+1

ありがとうございました!私はあなたの答えを受け入れ、私は14時間以内にそれを授与する – EnexoOnoma

関連する問題