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 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
  • 3 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

Related Questions

I have two mysql tables. jobsreqtraining and coursescompleted. jobsreqtraining has two columns, job and
I have a mysql database with two tables, its a one-to-many relationship. table1 has
I have two tables suppose table 1 has two columns with short names and
I have two tables pertaining to this question: conversations has many messages . The
I have two tables called categories and topics. categories has columns category_id and category_title.
I have two MySQL tables A and B . Table A has a member
I have two tables user and location user has id(primary) and location fields/columns location
I have two MySQL tables, with the following structure (I have removed irrelevant columns).
I have two tables, posts and pages . The posts table has more columns
using php and mysql I have two tables, a users table and a profiles

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.