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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:51:40+00:00 2026-06-04T13:51:40+00:00

This is kind of hard to explain so I’ll break it down…Here’s the objective

  • 0

This is kind of hard to explain so I’ll break it down…Here’s the objective

Suppose you have a table

ID | Weight

1     2
2     4
3     8
4     66
5     11 
6     44
7     33

And suppose I have a set of interested IDs, say (3,4)

My objective is to get two other rows (one for each of the two interested IDs) such that the row that matches with the interested ID has a weight that is one level less than the weight of the interested ID

so in this case

for id 3, we want to return row with ID 2 and weight 4 since row id 2 is the first row of which the weight (4) is less than the weight of row id 3 (8)

for id 4, we want to return row with id 6 and weight 44 since row id 6 is the first row of which the weight (44) is less than the weight of row id 4 (66)

How would you accomplish this with mysql in one single query whereby we use the IN() notation for the interested IDs…..

  • 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-04T13:51:41+00:00Added an answer on June 4, 2026 at 1:51 pm

    I’d like to propose the following (used ourtable as table name obviously)

    SELECT id,weight FROM ourtable WHERE weight IN (SELECT MAX(t.weight) FROM ourtable t,ourtable t2 WHERE t.weight < t2.weight && t2.id IN (3,4) GROUP BY t2.id);
    

    it gives the following result

    +----+--------+
    | id | weight |
    +----+--------+
    |  2 |      4 |
    |  6 |     44 |
    +----+--------+
    

    as requested.

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

Sidebar

Related Questions

This is kind of hard to explain. I have an absolutely positioned floating secondary
it's pretty simple but is kind of hard to explain. I have this image
This is kind of hard to explain. Here's an example of my HTML: <div
So this is kind of hard to explain in text, I have attached an
This is kind of hard to explain, but here is my problem: When, at
This is kind of hard to explain, so bear with me. In PHP, if
This is kind of hard to explain, so I'll do my best: I want
This is kind of hard to explain but I'll do what I can. I
This is kind of hard to explain. Basically it's kind of like a simple
This is kind of hard to explain, I hope my English is sufficient: I

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.