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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:49:27+00:00 2026-05-25T00:49:27+00:00

I have a bit of a strange problem, please examine the following SQL CREATE

  • 0

I have a bit of a strange problem, please examine the following SQL

CREATE TABLE `tablea` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `name` VARCHAR( 60 ) NOT NULL
) ENGINE = MYISAM ;

CREATE TABLE `tableb` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `name` VARCHAR( 60 ) NOT NULL ,
    `refid` INT NOT NULL ,
    `position` INT NOT NULL
) ENGINE = MYISAM ;

INSERT INTO tablea (`id`,`name`) VALUE (1,'a'),(2,'b'),(3,'c');
INSERT INTO tableb (`name`, `refid`, `position`) VALUE ('a', 1, 2),('b', 1, 1);
INSERT INTO tableb (`name`, `refid`, `position`) VALUE ('a', 2, 1),('b', 2, 2);

Tableb holds 0 or more rows referencing tablea. I want to recall the records in tablea with the record in tableb that has the lowest (MIN) position. So at the moment this is a simple bit of SQL.

SELECT 
    a.id, 
    a.name AS namea,
    b.name AS nameb,
    GROUP_CONCAT(b.name),
   CAST(group_concat(b.position) AS CHAR )
FROM tablea AS a
LEFT JOIN tableb AS b
    ON b.refid=a.id
GROUP BY a.id
ORDER BY b.position ASC

If you run this you will see it output

id  namea   nameb   concat  concat
3   c   NULL    NULL    NULL
1   a   b   b,a 1,2
2   b   b   b,a 2,1

The first & second row is correct but the third I was expecting nameb to be a and not b. I have tried playing around with MIN but cannot seam to get something that always returns me what I am expecting. Any help appreciated, at the moment I am just having to do 2 queries 🙁

  • 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-25T00:49:28+00:00Added an answer on May 25, 2026 at 12:49 am
    SELECT 
        a.id, 
        a.name AS namea,
        b.name AS nameb,
    FROM tablea AS a
    LEFT JOIN tableb AS b
        ON b.refid=a.id AND b.position = (SELECT min(position) FROM tableb b2 WHERE b2.rf_id = a.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experiencing something a bit strange. I have a table on SQL Server 2008,
I have a bit of a strange problem. I am trying to send in-app
I have a bit of strange problem on this page: http://www.bluprintliving.com/locations it seems that
I have a feeling my problem is a bit strange, but here goes... I
I have a bit of a strange problem with MySQL that I've never seen
I have bit of a strange problem. In have a MovieClip i resize, but
I'm a bit newbie to Flash, and I'm experiencing strange problem. I have a
I've have this strange problem when adding a column to an existing table. The
I have a bit of a strange question. Can anyone tell me where to
This a bit of strange one.... We have an internal web app that runs

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.