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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:43:09+00:00 2026-06-11T11:43:09+00:00

I have two tables in my database Table A And Table B – Table

  • 0

I have two tables in my database Table A And Table B –

                   Table `A`
    SN   |    Order1    |   Order2   |   Text
 (INT)   |   (TINYINT)  |  (TINYINT) |  (VARCHAR)
   1001  |      1       |      1     |    ABC
   1001  |      1       |      2     |    DEF
   1001  |      1       |      3     |    GHI
   1001  |      2       |      1     |    IOU
   1001  |      3       |      1     |    JKL     <--
   1001  |      3       |      2     |    LMO
   1001  |      3       |      3     |    UTF
   ....
   1021  |      1       |      1     |    ZXC
   1021  |      1       |      2     |    QWE
   1021  |      2       |      1     |    JKL     <--
   1021  |      3       |      1     |    YOU

And In Another Table

                   Table `B`
    SN   |    Order1    |     rSN    |   rOrder1
   1021  |      2       |     1001   |      3

Now when I query for the data of 1021 the result should be like :-

         Result Needed

    1    |      1     |    ZXC
    1    |      2     |    QWE
    2    |      1     |    JKL
    2    |      2     |    LMO
    2    |      3     |    UTF
    3    |      1     |    YOU

Presently I am trying somthing Like this –

SELECT `SN`, `Order1`, `Order2`, `Text` FROM `Table A` 
  WHERE `SN`=1012 
UNION  
SELECT `SN`, `Order1`, `Order2`, `Text` FROM `Table A`  
  WHERE `Table A`.`SN` 
  IN (SELECT  `rSN` FROM `Table B` WHERE `SN`=1021) 
  AND `Table A`.`Order1` 
  IN (SELECT  `Order1` FROM `Table B` WHERE `SN`=1021) 

Which Is giving the Results Like this :-

             Result
  SN | Order1 |   Order2   |    Text
 1021|   1    |      1     |    ZXC
 1021|   1    |      2     |    QWE
 1021|   2    |      1     |    JKL
 1021|   3    |      1     |    YOU
 1001|   3    |      1     |    JKL
 1001|   3    |      2     |    LMO
 1001|   3    |      3     |    UTF

What should I do to get the Order1 and Order2 of the last three rows of the result be same as the referring Row i.e. like 1001 | 2 | 2 | LMO

Edit —

Here I am Thinking to get the Order1 values of the 1001 to be same as Order1 of 1012 when query gives the data output.

As The Order Of the Text Is important.

The Order2 Text is related to its corresponding first value in this Group In Order1
And Table B Stores reference to the already entered duplicate related Text in the database and defines its position in the corresponding SN

  • 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-11T11:43:11+00:00Added an answer on June 11, 2026 at 11:43 am

    I found the solution myself : –

    Solution

    SELECT `Order1`, `Order2`, `Text` FROM `Table A` WHERE `SN` = 1012
    UNION DISTINCT
    SELECT `Table B`.`Order1`,  `Table A`.`Order2`, `Table A`.`Text` 
    FROM  `Table A` JOIN  `Table B` 
    WHERE  `Table B`.`rSN` =  `Table A`.`SN` 
          AND  `Table B`.`Order1` =  `Table A`.`Order1` 
          AND `Table B`.`SN` = 1021
    ORDER BY `Order1`, `Order2`
    

    Explanation

    The First SELECT Selects the Rows associated with 1021

    In Second Part of query, I wanted the Text Of the referred SN i.e. 1001‘s row’s having Order1 value 3(See Table B).
    But as the referring SN i.e. 1021 have its Order1 value 2, WHERE its Text is JKL (See Table A, Pointed Row (“<–“) with 1021 SN)
    So We want All the values of Order1 of Text associated with 1001 | 3 | Formatted As 1001 | 2 |
    For this above problem JOIN Works, Joining gives a table like :-

    SELECT *  FROM  `Table A` JOIN  `Table B` 
    WHERE  `Table B`.`rSN` =  `Table A`.`SN` 
      AND  `Table B`.`Order1` =  `Table A`.`Order1` 
      AND `Table B`.`SN` = 1021
    
                Table `B`                            Table `A`
     SN  | Order1 |  rSN | rOrder1     ||    SN | Order1 | Order2 | Text
    1021 |   2    | 1001 |    3        || 1001  |    3   |    1   | JKL 
    1021 |   2    | 1001 |    3        || 1001  |    3   |    2   | LMO
    1021 |   2    | 1001 |    3        || 1001  |    3   |    3   | UTF
    

    And From this result we can Pick up the result set.
    Understanding how 2, 2, LMO comes -> It is here Now we are picking up the Table B.Order1 From the JOIN result Instead like earlier we were Picking up the Order1 Of the 1001 instead, which then results into ordering which we didn’t wanted.

    why isn't 1, 3, GHI required as a part of result -> As Its Like Related Information,
    The Information with same Order1 show the related words/information to each other and since 1021 Do not have any related information to 1, 3, GHI Hence It is not required in the result part.

    And as Now we already getting the Order1 of the second query same as it should be 2 not 3 We can now easily Order Them by Order clause

    I hope this will clarify the question also.
    Still Thanks everyone 🙂

    —— EDIT ——-

    DISTINCT in UNION will remove the Duplicate Entry of JKL From the final results

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

Sidebar

Related Questions

I have two tables in database: comments table users table | u_id | text
I have two tables in my database Table:Documents Id (int), DocName (nvarchar) ---------------------------- Table:AccessLogs
I have two tables in my database: user and media_contact . The media_contact table
I have a database framework where I have two tables. The first table has
In a MySQL database I have two tables linked in a join. One table
I have a database two tables and a linking table that I need a
I have a very simple database table that joins two other tables in a
What I have is two tables inside of a mysql database. One table contains
I have the following two tables in my mysql database. Table Name: groups id
I Have a database called 'lms' with two tables loan and value, table loan

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.