Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not clear if you are wanting the users that has an object with the most likes listed, or whether you are looking for the users that have the most overall likes spread over all their content. </p> <p>Consider a user who has just one photo which has 1,000 likes, should that user get listed before a user with a total of 100 photos, videos and posts with an average like count of 20, with the highest number of likes at 50?</p> <p>Those are two different queries.</p> <p>For now, I'm going to assume that what you want to find is the "most liked" items, and then find which users each of those objects was created/authored by. To find the 20 "most liked" items, it's pretty straightforward to find which "<code>obj_id</code>"s have the most likes...</p> <pre><code>SELECT l.obj_id , COUNT(1) AS like_count FROM likes_service.likes l GROUP BY l.obj_id ORDER BY like_count DESC LIMIT 0,20 </code></pre> <p>I'm assuming (based on your original query), and absent a schema and sample data, that the values of <code>obj_id</code> in the <code>likes</code> table reference an <code>id</code> value of a single object in one of the other tables... that is, an <code>obj_id</code> value won't appear in both the <code>photos</code> and <code>videos</code> table. (Otherwise, you'd probably have a column somewhere next to <code>obj_id</code> to tell you which table the <code>obj_id</code> was referencing.)</p> <p>We use that previous query as an inline view (MySQL calls it a derived table), and give it a convenient alias of "ml" (most liked), and we'll do a LEFT JOIN to each of the target object tables (photos, videos, posts), to figure out what kind of item it is, and who the user/creator/author is.</p> <pre><code>SELECT ml.obj_id , ml.like_count , p.user_id , v.creator_id , t.author_id FROM ( SELECT l.obj_id , COUNT(1) AS like_count FROM likes_service.likes l GROUP BY l.obj_id ORDER BY like_count DESC LIMIT 0,20 ) ml LEFT JOIN prod.pictures p ON p.id = ml.obj_id LEFT JOIN prod.videos v ON v.id = ml.obj_id LEFT JOIN prod.videos v ON v.id = ml.obj_id LEFT JOIN prod.posts t ON t.id = ml.obj_id ORDER BY ml.like_count DESC </code></pre> <p>Assuming that the <code>user_id</code> column from the photos table is NOT NULL, and the <code>creator_id from the</code>videos` table is NOT NULL ...</p> <p>Then you can determine which table the obj_id was found in. i.e. if the <code>user_id</code> column is not null, you know it's from the photos table, if the <code>creator_id</code> is not null, you know its from the videos table.</p> <p>You could add some expressions in that outermost select to decipher that...</p> <pre><code>SELECT CASE WHEN p.user_id IS NOT NULL THEN 'photo' WHEN v.creator_id IS NOT NULL THEN 'video' WHEN t.author_ID IS NOT NULL THEN 'post' END AS obj_type , CASE WHEN p.user_id IS NOT NULL THEN p.user_id WHEN v.creator_id IS NOT NULL THEN v.creator_id WHEN t.author_id IS NOT NULL THEN t.author_id END AS user_id , l.obj_id </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload