LoginSignup
2
2

More than 5 years have passed since last update.

サブクエリを使ってログをカウントし、アクセスが多い上位5件を拾ってくる。

Posted at

サブクエリを使ってログをカウントし、アクセスが多い上位5件を拾ってくる。

SQL

select (select count(id) from point_logs where point_id = points.id) as log_count, questions.id from questions LEFT JOIN points on questions.point_id = points.id order by log_count desc limit 5;

結果

 log_count | id 
-----------+----
         3 | 18
         3 |  2
         1 | 12
         0 | 11
         0 |  5

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2