1

I have a table called music_lib which has a bunch of columns and over 2000 entries.

id | track_id | total_plays | total_downloads | likes | active | etc | etc

I'm trying to select only the most recent 50 active entries, based on id but I want to order them relative to the combined values (all ints) of the following columns :

total_plays + total_downloads + likes

So basically I want the latest 50 entries ordered by how popular they are (popularity being based on the combined plays, downloads and likes of each entry).

But I have no idea how to formulate this query with PHP / MySQL (using wordpress)..

I know I can get the most recent 50 entries with the following but no idea how to order those results based on the combined figure of the three columns as mentioned above.

$getpodcasts = $wpdb->get_results( "SELECT * FROM music_lib WHERE active = 1 ORDER BY id DESC LIMIT 50");
2
  • Track_id + x ??? What? Commented Jan 4, 2017 at 19:19
  • Sorry track_id should have been 'likes', my bad, will correct the question Commented Jan 7, 2017 at 20:01

1 Answer 1

1

then use order by and your calculation and then id like below-

$getpodcasts = $wpdb->get_results( "
    SELECT * FROM music_lib 
    WHERE active = 1 
    ORDER BY (track_id + total_plays + total_downloads) DESC,
             id desc 
    LIMIT 50");

first it looks for track_id + total_plays + total_downloads and sort them accordingly and then sort them by id

Sign up to request clarification or add additional context in comments.

4 Comments

let me know whether it works for you or not... if helpful please accept as answer and upvote to
Thanks for your reply, this partially works. It shows only 50 in the correct order, but it is calculating the sum of the 3 columns from EVERY entry in the table, not JUST the last 50 which I am trying to do. Any ideas?
you can remove - limit 50
actually i could not understand your need ... thanks for your reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.