Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:02:24+00:00 2026-06-10T05:02:24+00:00

I need to create a mysql query for my project that’s a bit too

  • 0

I need to create a mysql query for my project that’s a bit too complicated for my scope…

So, I a table of images with id and timestamp columns, along with metadata columns

I also have a table of “loves”, which has columns for id, imageid, userid, and timestamp

(userid not really important here)

Currently, I am using a LEFT JOIN to sort the images by their total number of likes

What I would like to do now is, instead, sort the images by their daily average of likes.

So, an image created today that has 5 likes associated with it should come before an image created 5 days ago with 20 likes associated.

Not even sure how to begin to approach this, any of you SQL gurus have any ideas? Cheers.

EDIT:

Using this query

SELECT images.*, 
COUNT(loves.id) AS num_loves 
FROM images 
JOIN loves ON (images.id = loves.imageid) 
GROUP BY images.id 
ORDER BY num_loves/DATEDIFF(images.timestamp,CURDATE()) 
DESC LIMIT 0 , 24

getting this error

Reference 'num_loves' not supported (reference to group function)

Still getting a handle on MySQL syntax…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:02:25+00:00Added an answer on June 10, 2026 at 5:02 am

    You can use any valid expression as your ORDER BY clause. This means we just need to recall a hint of algebra:

    SELECT 
      images.url,
      images.date_added
    FROM IMAGES
    JOIN image_likes ON image_likes.image_id = images.id
    GROUP BY images.id
    ORDER BY count(image_likes.id)/DATEDIFF(CURDATE(), images.date_added)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a MySQL query that generates 4 rows for each row
I need to create a table in MySQL which stores the different currency symbols
I need to create pdf document which outputs all rows from mysql table, but
In MySQL, I need to create a new table called users with the following
I need to create MySQL table. So I'm running this script but it's just
I really need some help with forming a MySQL query that I just cannot
Im trying to create a function, that will return a mysql query, which i
I need to create a simple mySQL database with a field for an image.
I'm using Rails 3 with a MySQL database, and I need to programmatically create
UPDATED: I am using MySQL statement to create a view: I need to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.