Hello!
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!