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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:30:05+00:00 2026-06-07T12:30:05+00:00

I have data in two tables. The first table has a Primary Key called

  • 0

I have data in two tables.

The first table has a Primary Key called PKID

PKID  DATA
0    myData0
1    myData1
2    myData2

The second table has the PKID column from table 1 as a foreign key

PKID_FROM_TABLE_1  U_DATA
       0          unique0
       0          unique1        
       0          unique2
       1          unique3
       1          unique4
       1          unique5
       2          unique6
       2          unique7
       2          unique8

The basic SELECT statement I am making now is

SELECT a.PKID, a.DATA, b.U_DATA
FROM table1 as a
INNER JOIN table2 as b
ON a.PKID = b.PKID_FROM_TABLE_1

This produces a table like this:

PKID   DATA     U_DATA
 0   myData0    unique0
 0   myData0    unique1
 0   myData0    unique2
 1   myData1    unique3
 1   myData1    unique4
 1   myData1    unique5
 2   myData2    unique6
 2   myData2    unique7
 2   myData2    unique8

What I would like is the following table:

PKID   DATA    U_DATA1    U_DATA2    U_DATA3
 0     myData0 unique0    unidque1   unique2
 1     myData1 unique3    unidque4   unique5
 2     myData2 unique6    unidque7   unique8

If it helps, each PKID will have exactly 3 entries in table2.

Is something like this possible in MySQL?

  • 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-07T12:30:07+00:00Added an answer on June 7, 2026 at 12:30 pm

    This is one way to get the result.

    This approach uses correlated subqueries. Each subquery uses an ORDER BY clause to sort the related rows from table2, and uses the LIMIT clause to retrieve the 1st, 2nd and 3rd rows.

    SELECT a.PKID
         , a.DATA
         , (SELECT b1.U_DATA FROM table2 b1
             WHERE b1.PKID_FROM_TABLE_1 = a.PKID 
             ORDER BY b1.U_DATA LIMIT 0,1
           ) AS U_DATA1
         , (SELECT b2.U_DATA FROM table2 b2
             WHERE b2.PKID_FROM_TABLE_1 = a.PKID 
             ORDER BY b2.U_DATA LIMIT 1,1
           ) AS U_DATA2
         , (SELECT b3.U_DATA FROM table2 b3
             WHERE b3.PKID_FROM_TABLE_1 = a.PKID 
             ORDER BY b3.U_DATA LIMIT 2,1
           ) AS U_DATA3
      FROM table1 a
     ORDER BY a.PKID  
    

    FOLLOWUP

    @gliese581g points out that there may be performance issues with this approach, with a large number of rows returned by the outer query, since each subquery in the SELECT list gets executed for each row returned in the outer query.

    It should go without saying that this approach cries out for an index:

    ON table2 (PKID_FROM_TABLE_1, U_DATA)
    

    -or, at a minimum-

    ON table2 (PKID_FROM_TABLE_1)
    

    It’s likely the latter index already exists, if there’s a foreign key defined. The former index would allow the query to be satisfied entirely from the index pages (“Using index”), without the need for a sort operation (“Using filesort”).

    @glies581g is quite right to point out that performance of this approach can be problematic on “large” sets.

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

Sidebar

Related Questions

I have two tables. The first table holds simple user data and has the
i have two data tables with the same structure the first one has one
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
I have a two tables. TABLE 1 has id(PRIMARY)|RANK|WEBSITE |ADDRESS This is filled with
I have two tables. Most of the data is coming from the first table
I want to get data from two tables. I have patient first name and
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
I have two tables set up in phpmyadmin- table userid and table data. The
Suppose I have two tables: Group ( id integer primary key, someData1 text, someData2
I have two tables in my database. The first one, [nodeActivity] has the following

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.