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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:01:05+00:00 2026-06-17T10:01:05+00:00

I have two tables that can have multiple records linking to another table, but

  • 0

I have two tables that can have multiple records linking to another table, but I don’t want mySQL to combine rows together. Example:

  • test_main contains fields mainID, field1.
  • test_veg contains a record with a name of a vegetable, linked to the record in test_main with ID=1
  • test_fruit contains two records each with a name of a fruit, both linked to the record in test_main with ID=1

In this example there are three records that link to test_main – one test_veg and two test_fruit. I want to get those 3 rows to give a result like:

field1               vegName  fruitName
-------------------  -------  ---------
stuff in main table  cabbage  NULL
stuff in main table  NULL     apple
stuff in main table  NULL     pear

I would also like records from test_main that don’t have any test_veg or test_fruit records linked to them.

This seems so simple but I can’t get it to work. Any ideas?

If I only had two tables (e.g. test_main and test_veg), a left join would work. With 3 tables, two left joins return only two rows:

SELECT  test_main.field1, test_veg.vegName, test_fruit.fruitName
FROM test_main
LEFT  JOIN test_veg ON test_veg.mainID = test_main.mainID
LEFT  JOIN test_fruit ON test_fruit.mainID = test_main.mainID
WHERE test_main.mainID=1

field1               vegName  fruitName
-------------------  -------  ---------
stuff in main table  cabbage  apple
stuff in main table  cabbage  pear

NB I’m stuck with mySQL3, which means no fancy things like SELECTs within WHEREs, nor UNIONs.

  • 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-17T10:01:07+00:00Added an answer on June 17, 2026 at 10:01 am

    There is a very cool article I read a while back about tricking MySQL3.x into mimicking a UNION. You need to create a DUMMY table with as many rows as needed for the Unions. So if you have 1 union (which is 2 different select statements), you’d need 2 rows in the dummy table.

    Try something like this:

    SELECT m.field1, v.vegName, f.fruitName
    FROM test_main m
    INNER JOIN dummy d 
      ON d.id < 3
    LEFT JOIN test_veg v 
      ON v.mainID = m.mainID 
      and d.id = 1
    LEFT JOIN test_fruit f 
      ON f.mainID = m.mainID 
      and d.id = 2
    WHERE m.mainid=1
    

    Here is the SQL Fiddle to better help you follow along.

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

Sidebar

Related Questions

I have two database tables Articles Archive Each article can have multiple records in
I have one table that has two fields - ID1 and ID2 ID1 can
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have two tables that are related via a mapping table: keywords titles I
I have two tables that I need to combine to get the data I
I have a 1 to many relation between two tables that I want to
I have two simple tables in my database. A card table that contains Id,
I have two tables that link together through an id one is submit_moderate and
I have two tables that have virtually identical content and very similar structure. They
I have two tables that should be joined together by a foreign key relationship,

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.