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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:05:20+00:00 2026-06-18T04:05:20+00:00

If I have a table like this: TABLEA : id| object | type 1

  • 0

If I have a table like this:

TABLEA:

id| object | type
1 | greg | person
2 | mary | person
3 | jared | person
4 | kelly | person
5 | melissa | person
6 | william | person
7 | skiing | hobby
8 | biking | hobby

TABLEB:

id | husband | wife
1 | greg | mary
2 | jared | kelly
3 | william | kelly

TABLEC:

id | female | hobby
1 | mary | skiing
2 | kelly | biking

Is there some way I could get a result table of:

TABLED:

id | a | b | link
1 | 1 | 2 | related
2 | 3 | 4 | related
3 | 6 | 4 | related
4 | 1 | 4 | related
5 | 2 | 7 | likes
6 | 4 | 8 | likes

Using only MySQL query(/ies)?

  • Logic would basically start iterating from TABLE B and all rows of table B.
  • The third column is related when the table being selected is TableB, and is likes when the table being processed is TABLE B.

Sample logic would be:

Looking at the first row of TABLEB (husband) greg and wife (mary), it looks up from TABLEA to see that greg is at row 1 (id 1) and mary is at row 2 (id 2) and creates a new TableD with the first row being 1 | 2.

Is there a query that can do some join or something that would be able to do this without having to programmatically iterate through all rows of TABLEB, then all rows of TABLEC to produce the desired TABLED?

  • 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-18T04:05:21+00:00Added an answer on June 18, 2026 at 4:05 am

    You will need to JOIN the table tablea two times with the tableb:

    • one for husband > object and another,
    • one for wife > object.

    So that you can get the two ids of the husband and wife in the same row.

    The same with Tablec with tablea. Then use UNION(implicit distinct) or UNION ALL to union the two result sets.

    Something like:

    SELECT
      (@rownum := @rownum + 1) AS id,
      sub.*
    FROM
    (
      SELECT 
        ah.id     AS a,
        aw.id     AS b,
        'related' AS link
      FROM tableb       AS b
      INNER JOIN Tablea AS ah ON ah.object = b.husband AND ah.type = 'person'
      INNER JOIN Tablea AS aw ON aw.object = b.wife    AND aw.type = 'person'
      UNION ALL
      SELECT
        a.id  AS a,
        a2.id AS b,
        'hobby'
      FROM tablec       AS c
      INNER JOIN tablea AS a  ON a.object  = c.female 
      INNER JOIN tablea AS a2 ON a2.object = c.hobby AND a2.type = 'hobby'
    ) AS Sub, (SELECT @rownum := 0) AS t;
    

    SQL Fiddle Demo

    This will give you:

    | ID | A | B |    LINK |
    ------------------------
    |  1 | 1 | 2 | related |
    |  2 | 3 | 4 | related |
    |  3 | 6 | 4 | related |
    |  4 | 2 | 7 |   hobby |
    |  5 | 4 | 8 |   hobby |
    

    Note that:

    • This query will give you only 5 rows, where the expected result set that you are looking for is 6. It is missing the row 4 | 1 | 4 | related, because there is no entry for those greg(id 1) and keyll (id 4) in the tableb. As you explained in your question.

    • The new column id is auto incremental id, generated from the result set, it is not selected from the tables.


    If you need to create a brand new table from this select, use the following syntax:

    CREATE TABLE Tabled
    AS
    SELECT ...
    ... -- the same previous select query
    

    and you will have a new table tabled having the same structure of this SELECT.

    Like in this updated fiddle.

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

Sidebar

Related Questions

I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
i have a table like this: Table(MissioneID, Type) Type can be 1,2 or 3
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
So I have a table like this Table 1 Quote Ref | Product A
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
I have a table like this: ID country ------------- 1 US 2 Japan 3
I have a table like this: id | id_parent | tag_name 0 | |

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.