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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:40:05+00:00 2026-05-11T13:40:05+00:00

In MySql, I have two tables, A and B. A has as columns A.id,

  • 0

In MySql, I have two tables, A and B.

A has as columns A.id, B has as columns B.id and B.aid.

or each row of A I have many rows of B. And the value of B.aid=A.id

of course.

Now I need to get a list of the values in A, but I need to order them, according to B.

In particular if I have two rows in A: a1 and a2. Each will have a series of rows in B: b11, b12, b13, … and b21, b22, b23, … Now I need to order the A from the one connected with the highest b.id to the one with the second highest, and so on. (of course having one row appearing only once).

I tried this:

SELECT a.id  FROM a, b WHERE a.id=b.aib ORDER BY b.id DESC  

I did indeed got all the values in the right order, but each value of A would appear n times, if n was the number of rows in B related to that row in A.

How do I avoid that, so that I get only one value.

I am considering taking the wholelist, and then eliminating all the non unique values, but I fear that once the website becomes big it might not be doable anymore.

(In case you wonder, this is to program a fac-simile of a discussion board, the table A is the thread, and the table B is the entry, and I want to have a page where all the threads are presented, but in order of the thread that had the last action later)

Many thanks, Pietro

P.S. MySql is not my thing, so please do spell out the solution 🙂

UPDATE: The actual code is more complex, as it also involves users, and similar. So I am looking at something like:

SELECT DISTINCT a.id, a.question, a.roundid, a.phase, users.username, users.id   FROM a, users, b  WHERE a.phase = 0 AND users.id = a.usercreatorid AND b.experimentid = a.id  ORDER BY b.id DESC 

I tried the DISTINCT, as suggested below, but it does not work. I do get all the thread (i.e. questions) uniquely, but thhey are not perfectly ordered. I do not know why, but it seem he is not chosing a random row from b, and this goes generally in the right direction, but it is not the row with the max(b.id). SO the distinct does not sort between rows in the correct way. I will now look at the other solutions proposed.

  • 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. 2026-05-11T13:40:05+00:00Added an answer on May 11, 2026 at 1:40 pm

    As I understand it, you want the a.id values, ordered by the most recent corresponding b.id value.

    Where you have a 1->many relation and need that sort of info, you’re typically looking at a GROUP BY to aggregrate the data, or a subquery for more complex criteria.

    So, something like this should do it, using a group by:

    SELECT     a.id, a.question, a.roundid, a.phase,     users.username, users.id,     MAX(b.id) AS latest  FROM     a, users, b  WHERE     a.phase = 0 AND users.id = a.usercreatorid AND b.experimentid = a.id  GROUP BY a.id ORDER BY latest DESC 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I found the solution. Probably has to do with OSX… May 11, 2026 at 3:15 pm
  • added an answer ASP.NET only changes the IDs when the control is inside… May 11, 2026 at 3:15 pm
  • added an answer ClassLoader.getResourceAsStream(). As stated in the comment below, if you are… May 11, 2026 at 3:15 pm

Related Questions

I have two tables A and B. I would like to delete all the
I am using Jython 2.2.1 and MySQL Connector/J 5.1 to access a MySQL database.
Hey, I stumbled upon this site looking for solutions for event overlaps in mySQL
Google results on this one are a bit thin, but suggest that it is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.