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

  • Home
  • SEARCH
  • 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 8262885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:54:28+00:00 2026-06-08T03:54:28+00:00

I’m working on a project that will use a comparison voting logic to sort

  • 0

I’m working on a project that will use a comparison voting logic to sort the highest rated to the top and lowest to the bottom(Similar to a “hot or not” or “Hotstagram”). Basically what I need to do is take 2 random pictures that are directly next to each other in the database and have users vote, adding one point to the winner and subtracting on point from the loser essentially filtering the highest to the top.

My question is two fold, How can I get a random item in a MySql database then get a random item directly next to it. and secondly (if anyone has prior experience with this) how would you structure your DB? I’m thinking One table for images and a second that will hold the votes(then compile the results on page load). I guess what my concern is here, for the ranking, ever new entered photo will start at zero, so you could have X amount of photos with the same rank? I’m just throwing some thoughts out there and I need another mind to toss this around.

  • 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-08T03:54:30+00:00Added an answer on June 8, 2026 at 3:54 am

    You can use the Order by Rand() function to get a random record from the database.

    SELECT * FROM tbl_name ORDER BY RAND();

    You can also limit this to two rows easily enough as such:

    SELECT * FROM tbl_name ORDER BY RAND() limit 2;

    This will however get two random rows from the db, not two consecutive rows.

    If you wanted to get two random AND consecutive rows, you could run a query to get the ID and a subquery to get the next ID after it.

    As for keep data in one/two tables, definately keep it in two tables. Link your second table that keeps the votes with a link to the ID of the image. Obviously add indexes as required on the linked (and or other fields) as needed.

    Edit: Here is the code to get you the two subsequent rows of data in one row then:

    select
        a.id,
        (
            select
                min(c.id)
            from
                table1 c
            where
                c.id>a.id
            limit 1
        ) as id2
    from
        table1 a
    order by
        rand()
    limit 1;
    

    Edit 2: If you wanted them as separate rows to pull in all sorts of other bits, I have provided the query below. I used an extra subquery as if the initial query happened to pull out the max value (possible when using order by rand() then it solves the issue of only returning one row of data.

    select
        b.id
    from
        table1 b,
        (
        select
            a.id as id
        from
            table1 a
        where
            a.id<(select max(id) from table1 limit 1)
        order by 
            rand() 
        limit 1
        ) a
    where
        b.id>=a.id
        order by
        b.id
    limit 2
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

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.