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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:43:39+00:00 2026-06-05T11:43:39+00:00

I have two tables as given below Table A Table B Table C =============

  • 0

I have two tables as given below

    Table A                Table B             Table C              
  =============          ==============        =========
    Id    Name           Id   AId  CId         Id   Name 
    1     A              1    1    1           1    x
    2     B              2    1    1           2    y
    3     C              3    2    1           3    z 
    4     D              4    2    3           4    w
    5     E              5    3    2           5    v

Now I want all the records of Table A with matching Id column CId from Table B where CId = 1.

So the output should be like below :

Id Name CId
1   A   1 
2   B   1
3   C   1
4   D   Null
5   E   Null

Can anyone help me please?

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

    This does what you want:

    SELECT
    A.Id,
    A.Name,
    CASE B.CId WHEN 1 THEN 1 ELSE NULL END AS CId
    FROM
    A LEFT JOIN B ON A.Id = B.Id
    

    This is not about LEFT JOINing. You could as well do it with an INNER JOIN. When you don’t want the 3 and 2 of column CId to appear you would still have to filter with WHERE and therefore the rows with Id 4 and 5 would not appear, which is not what you want.

    EDIT:

    Given this test data:

    create table A (Id int, Name varchar(5));
    insert into A values 
    (1, 'A'),
    (2, 'B'),
    (3, 'C'),
    (4, 'D'),
    (5, 'E');
    
    create table B (Id int, AId int, CId int);
    insert into B values
    (1,1,1),
    (2,1,1),
    (3,2,1),
    (4,2,3),
    (5,3,2);
    

    my query does not give a cartesian product. Read and try before downvoting. Anyway, it was not clear to me what you want to achieve, now I’ve joined on AId column and with this query:

    SELECT DISTINCT
    A.Id,
    A.Name
    , CASE 
    WHEN B.CId > 1 THEN 1 
    WHEN B.CId = 1 THEN 1
    ELSE NULL END AS CId
    FROM
    A LEFT JOIN B ON A.Id = B.AId
    

    and it also gives the right output, like the first before. If this is still not what you want, your test data is wrong or I absolutely don’t get it.

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

Sidebar

Related Questions

I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables, for example: Table A Table B ======= ======= Name |
Table Schema For the two tables, the CREATE queries are given below: Table1: (file_path_key,
I Have two tables 1. Class table 2. notification table class table contains all
I have two table masterTbl and detailTbl which structure is given below... --PRIMARY TABLE
Given a database with two tables X and Y , I have a query
I have two tables on a webform. On a button click I want to
I have two Tables Let suppose A and B Now suppose the structure of
I have two tables as below: Student: oneID (primary key), col1, col2, col3, subId,
i have two tables 'city' and 'info'. City looks like city_id name 1 c1

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.