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

  • Home
  • SEARCH
  • 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 154395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:58:17+00:00 2026-05-11T09:58:17+00:00

I am having a problem with MySQL joins. Table_A : A_id Cost1 A1_id Cost2

  • 0

I am having a problem with MySQL joins.

Table_A:

 A_id  Cost1  A1_id  Cost2  1     500     0     200  1     100     1     100  1     50      2      60  1     10      3      50  2      5      0      10 

Table_B (Refers B_id: from Table_A A1_id):

 B_id  FName  LName  1     X      A  2     Y      B  3     Z      C 

Table_C (Refers C_id: from Table_A A_id):

C_id  Towns   1    Atlanta  2    NewYork 

I need to combine all three tables, like the following output:

  • I extract the Towns that match (Table_A.A_id=Table_C.C_id).
  • I extract the Fname,Lname that match (table_A.A1_id=Table_b.b_id).
  • I need to skip the Towns if A1_id != 0.
  • I need to skip the Fname,Lname if A1_id == 0.

The remaining data may either be a value or null, which I specify as ‘#’. What would be an efficient MySQL query for the given scenario?

Output:

A_id  Cost1  A1_id cost2 Fname Lname Towns 1     500     0    200    #     #    Atlanta 1     100     1    100    X     A     # 1      50     2     60    Y     B     # 1      10     3     50    Z     C     # 
  • 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. 2026-05-11T09:58:17+00:00Added an answer on May 11, 2026 at 9:58 am

    This looks like a UNION of two distinct queries to me. I’m going to assume that the ID columns never contain negative values.

    SELECT A.A_id, A.Cost1, A.A1_id, A.Cost2, B.Fname, B.Lname, C.Town     FROM Table_A AS A               INNER JOIN Table_B AS B ON A.A1_id = B.B_id          LEFT OUTER JOIN Table_C AS C ON A.A_id  = C.C_id     WHERE A.A1_id != 0       AND C.C_id < 0 UNION SELECT A.A_id, A.Cost1, A.A1_id, A.Cost2, B.Fname, B.Lname, C.Town     FROM Table_A AS A          LEFT OUTER JOIN Table_B AS B ON A.A1_id = B.B_id               INNER JOIN Table_C AS C ON A.A_id  = C.C_id     WHERE A.A1_id = 0       AND B.B_id < 0; 

    I’m not completely confident about that formulation. There’s a chance that the conditions on B.B_id < 0 and C.C_id < 0 need to be associated with the corresponding ON clauses.

    There’s also a decent chance that using two left outer joins in a single SELECT with appropriate OR’d filters would achieve the correct result.

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

Sidebar

Related Questions

I'm having a bit of a problem with converting the result of a MySQL
I'm having a problem with my MySQL statement. I need a query that counts
I'm having a strange problem with MySQL and would like to see if the
Having a bit of a problem with mysql... Mysql 5.1 on windows 2008 server
I am trying to learn mysql and having some problem with updating/adding data to
I am having a problem with Hibernate. The primary key of ID in mysql
I am having problem in joint two subqueries in MySQL, e.g. (select * from
I'm having a small problem making a query in MySQL. I have the following
I'm having a problem with MySQL going to sleep because a PHP script takes
Recently, I've started having a problem with my SQL Server 2005 client running on

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.