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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:24:48+00:00 2026-05-13T17:24:48+00:00

Say I have 3 tables. Table1 contains client_id and client_name. Table2 contains client_id, client_catid_1,

  • 0

Say I have 3 tables. Table1 contains client_id and client_name. Table2 contains client_id, client_catid_1, and client_catid_2. These to tables are joined based on the, client_id. Now the third table, contains category_id and category_name. How can join the third table based on the category id’s and get the category_name? example:

Table 1:

client_id | client_name
-----------------------
121231231 | Some name
345234666 | Another Name
-----------------------

Table 2:

client_id | client_catid_1 | client_catid_2
-------------------------------------------
121231231 | 22             | 79
345234666 | 34             | 566
------------------------------------------

Table 3:

category_id | category_name
----------------------------
22          | category 22
34          | category 34
79          | category 79
566         | category 566
----------------------------

Then the output:

client_id | client_name | client_cat1 | client_cat2
---------------------------------------------------
121231231 | Some name   | category 22 | category 79
345234666 | Another     | category 34 | category 566
---------------------------------------------------

And the current query:

SELECT client.*,
cat1id.client_catid_1 as cat1,
cat2id.client_catid_2 as cat2
FROM tb_clients AS client
LEFT JOIN tb_clients_categories cat1id ON client.client_id = cat1id.client_id
LEFT JOIN tb_clients_categories cat2id ON client.client_id = cat2id.client_id
WHERE client.client_id = 65447

Also, I am not asking about database normalization or design. I only say this because people tend to miss your question and start trying to inform you of better database design. I understand this. I am working with something I did not originally put together and changing the DB is not an option!

Thanks in advance!

  • 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-05-13T17:24:48+00:00Added an answer on May 13, 2026 at 5:24 pm

    Use:

       SELECT a.client_id,
              a.client_name,
              c1.category_name AS client_cat1,
              c2.category_name AS client_cat2
         FROM tb_clients a
    LEFT JOIN tb_clients_categories b ON b.client_id = a.client_id
    LEFT JOIN TABLE C c1 ON c1.category_id = b.client_catid_1
    LEFT JOIN TABLE C c2 ON c2.category_id = b.client_catid_2
        WHERE c1.category_name = 'Something' 
           -- because you can't refer to a column (client_cat1) alias in the WHERE
    

    That will give you a list of clients, and optionally their category names. If a relationship does not exist between tb_clients_categories and tb_clients, the client name will still appear and the client_cat1 and client_cat2 will be null. If you don’t want this, remove the LEFT keyword on the JOINs for only clients with two categories associated to appear in the output.

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

Sidebar

Related Questions

I have two tables say Table1 and Table2 that contains the following column with
Lets say I have five tables named table1, table2 ... table5. I have already
I have two tables, lets say table1 and table2 with common columns, id and
Let's say I have the following tables: PartyRelationship EffectiveDatedAttributes PartyRelationship contains a varchar column
For example, let's say I have the following two tables: Table1: Id ---- 1
So lets say I have TABLE1 and TABLE2. CREATE TABLE TABLE1 ( first_id int
Let's say I have two databases with identical tables, but one database's tables contains
Say I have table1 which contains a field called Name and a field called
Ok say I have two tables Product and Sales The product contains the id,
Lets say I have 2 tables. The first table, table_1, contains each posted content

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.