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 9168783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:42:29+00:00 2026-06-17T15:42:29+00:00

My table contains votes of users for different items. It has the following fields:

  • 0

My table contains votes of users for different items. It has the following fields:

id, user_id, item_id, vote, utc_time

I understand how to get the last vote of #user# for #item#, but it uses subquery:

    SELECT votes.*, items.name, items.price
        FROM votes JOIN items ON items.id = votes.item_id
        WHERE user_id = #user# AND item_id = #item#
            AND utc_time = (
                SELECT MAX(utc_time) FROM votes
                WHERE user_id = #user# AND item_id = #item#
            )

It works, but it looks quite stupid to me… There should be a more elegant way to get this one record. I tried the approach suggested here, but I cannot make it work yet, so I’ll appreciate your help: How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?

There is a second part to this question: Count rows with DISTINCT(several columns) and MAX(another column)

  • 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-17T15:42:31+00:00Added an answer on June 17, 2026 at 3:42 pm

    You want just one row from the result, the one with MAX(utc_time). In MySQL, there is a LIMIT clause you can apply with ORDER BY:

    SELECT votes.*, items.name, items.price
        FROM votes JOIN items ON items.id = votes.item_id
        WHERE user_id = #user# AND item_id = #item#
        ORDER BY votes.utc_time DESC
            LIMIT 1 ;
    

    An index on either (user_id, item_id, utc_time) or (item_id, user_id, utc_time) will be good for efficiency.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My table contains votes of users for different items. It has the following columns:
I have one table contains field named source with varchar(max). That field has following
How to get the size of a record in MySql if the table contains
I have a mysql database that has 2 table. the first table contains the
A table contains four columns server, directory, usage and datetime. All servers has got
Say I have a voting table, where users can vote on values up, down,
User table contains the following attributes (dateOfBirth, race, gender, ...). We would like to
I have two tables. First table is je_addchoice , which contains fields like choiceid
assume my table contains following columns Roll_number numeric not null Subject1 varchar(40) null Subject2
Oracle 11g PERSON table contains both seller and buyer ID. How can I get

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.