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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:51:29+00:00 2026-06-01T12:51:29+00:00

I have two tables, tbl_foo and tbl_bar , and I want to join these

  • 0

I have two tables, tbl_foo and tbl_bar, and I want to join these tables with tbl_foo.foo_id = tbl_bar.foo_id in the on-clause. However, for each tbl_bar.baz_id there should be one row for each tbl_foo.foo_id (even if no such entry in tbl_bar exists). How do I write such query?

There’s more info on the schema and my desired result below.

  • Edit: Each row must have a foo_id and baz_id.
  • Edit 2: Added tbl_baz below.

Desired result

+--------+--------+--------+------------+
| bar_id | baz_id | foo_id | some_field |
+--------+--------+--------+------------+
|      1 |    101 |      1 | foo        |
|      2 |    101 |      2 | bar        |
|      3 |    101 |      3 | baz        |
|   NULL |    101 |      4 | bin        |
|      4 |    102 |      1 | foo        |
|   NULL |    102 |      2 | bar        |
|      5 |    102 |      3 | baz        |
|   NULL |    102 |      4 | bin        |
+--------+--------+--------+------------+

Table: tbl_foo

+--------+------------+
| foo_id | some_field |
+--------+------------+
|      1 | foo        |
|      2 | bar        |
|      3 | baz        |
|      4 | bin        |
+--------+------------+

Table: tbl_bar

+--------+--------+--------+
| bar_id | baz_id | foo_id |
+--------+--------+--------+
|      1 |    101 |      1 |
|      2 |    101 |      2 |
|      3 |    101 |      3 |
|      4 |    102 |      1 |
|      5 |    102 |      3 |
+--------+--------+--------+

Table: tbl_baz

+--------+
| baz_id |
+--------+
|    101 |
|    102 |
+--------+

SQL Schema

CREATE TABLE tbl_foo (
    foo_id INT,
    some_field VARCHAR(255),
    PRIMARY KEY (foo_id)
);

INSERT INTO tbl_foo VALUES
(1, 'foo'),
(2, 'bar'),
(3, 'baz'),
(4, 'bin');

CREATE TABLE tbl_bar (
    bar_id INT,
    baz_id INT,
    foo_id INT,
    PRIMARY KEY (bar_id, baz_id),
    FOREIGN KEY (baz_id) REFERENCES tbl_baz (baz_id),
    FOREIGN KEY (foo_id) REFERENCES tbl_foo (foo_id)
);

INSERT INTO tbl_bar VALUES
(1, 101, 1),
(2, 101, 2),
(3, 101, 3),
(4, 102, 1),
(5, 102, 3);

CREATE TABLE tbl_baz (
    baz_id INT,
    PRIMARY KEY (baz_id)
);

INSERT INTO tbl_baz VALUES
(101),
(102);
  • 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-01T12:51:30+00:00Added an answer on June 1, 2026 at 12:51 pm

    You may be looking for a query like this:

    UPDATE

    Based upon new tbl_baz:

    select y.bar_id, x.baz_id, x.foo_id, x.some_field
    from (
        select a.foo_id, a.some_field, b.baz_id
        -- Cross foo_id with all baz_id
        from tbl_foo as a, tbl_baz as b
    ) as x
        -- Get the bar_id where it exists for each foo_id/baz_id combo
        left join tbl_bar as y on x.foo_id = y.foo_id
            and x.baz_id = y.baz_id
    order by x.baz_id, x.foo_id
    

    This is based on the assumption that you want to see each foo_id for each baz_id regardless of what is in your many-to-many table.

    EXAMPLE of why you may not want this, or may want to update your many-to-many table instead:

    If we replace “foo” and “baz” with “person” and “car”, this query is essentially saying that every person owns every car. This may be the case, but it is certainly not represented in the “ownership” many-to-many table (bar).

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

Sidebar

Related Questions

I have joining two tables,There are hundreds of records in table a and there
I have these two tables: tbl_courses id user_id course_name 3 5 First Course 4
I have two tables. In the UniqueZips table there is a code field assigned,
I have two tables. tbl_Invoice tbl_Payment What I want is List of Pending/Partial Invoices
android noob... I have two tables, with a one to many relationship between country_tbl
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables images2 and image_data So the goal is to have 1
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4
I have two tables, lets say table1 and table2 with common columns, id and
I have two tables: table a ida valuea 1 a 2 b 3 c

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.