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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:52:00+00:00 2026-05-26T01:52:00+00:00

this should be an easy one, but I can’t seem to get mysql to

  • 0

this should be an easy one, but I can’t seem to get mysql to play ball.

I’m trying to build a list of projects that a user most recently voted for, to be used on that users profile page.

I have table of votes, containing uid(INT), project(INT), timestamp(INT)

And a table of projects whose id field matches the project field in the votes table.

My initial query was

SELECT * FROM projects WHERE id IN(SELECT project FROM votes WHERE uid=x ORDER BY timestamp DESC);

This gives a correct list, but the ordering is messed up. I also realised that I had forgotten to add DISTINCT project to the subquery, but for some reason it was giving distinct projects anyway.

SELECT project FROM votes WHERE uid=x ORDER BY timestamp DESC;

On its own gives the correct ordering as expected, but as soon as I query from that, the ordering is discarded.

I must not understand how nested statements work, could someone help, and also explain why the DISTINCT keyword is not necessary to get distinct project ids from the votes table!?

An example as requested;

The votes table contains individual votes, so if a user votes for a project three times, it might look like this;

uid     project    timestamp
34      9          10984
34      9          11093
34      9          11309

If that user (34 in the example) then voted for a different project, the votes table would now look like;

uid     project    timestamp
34      9          10984
34      9          11093
34      9          11309
34      21         12612

The indicies 9 and 21 are then used to query the projects table. So for example the projects table might contain;

id     name           description
9      Project A      This is project A.
21     Project B      This is project B.

What I want is the list of projects that the user most recently voted for. So in this case I want to grab projects 21 and 9 from the projects table in that order, 21 then 9 and on into projects that the user voted for further into the past.

  • 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-05-26T01:52:01+00:00Added an answer on May 26, 2026 at 1:52 am

    I would use a join here instead of a subquery.

    SELECT p.*
        FROM projects p
            INNER JOIN votes v
                ON p.id = v.project
        WHERE v.uid = x
        ORDER BY v.timestamp DESC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be easy to find out but I can't seem to find it
This seems like it should be easy, but I can't get the right syntax.
This should be an easy one, but I can't find a good example anywhere.
This seems like it should be an easy one but I can't figure it
I feel like this should be an easy question but I can't seem to
This one should be easy. I just can't figure out what to search for...
I think this should be a very easy one, but I cant find the
This should be an easy one, but I'm not finding much online: I have
This should hopefully be an easy solution but I can't think of how to
This should be an easy one, but through all the research I've done on

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.