How can i join tables using column value?
I have three tables as listed below:
messages_table
-----------------------------------------------------------------------------
msg_id | msg_sub | msg_to | to_user_type | msg_from | from_user_type
-----------------------------------------------------------------------------
001 | test | 88 | manager | 42 | admin
002 | test2 | 88 | manager | 94 | manager
admin_table
-------------------------—
admin_id | admin_name
-------------------------—
001 | Super Admin
manager_table
---------------------------
manager_id | manager_name
---------------------------
88 | Mandela
94 | Kristen
How can i get the desired output as shown below with SQL query. I.e. Join tables with respect to column values when the following criteria is met:
If user_type = admin
then it should join with admin_table
.
If user_type = manager
then it should join with manager_table
.
Desired output:
-----------------------------------------------------
msg_id | msg_sub | msg_to_name | msg_from_name
-----------------------------------------------------
001 | test | Mandela | Super Admin
002 | test2 | Mandela | Kristen
I.e. Get the join sql query based on column value.
EDIT:
I want to fetch the datafrom sql query not form the serverside coding.
I tried this query from here, i.e. Winfred's Idea ( Answered )
However, I could not understand it.
msg_by_usertype
is the column based, where the value manager
then it should select manager_table
and if it is admin the to admin_table
Join tables with respect to column values when the following criteria is met: If user_type = admin then it should join with admin_table. If user_type = manager then it should join with manager_table. I.e. Get the join sql query based on column value.
RIGHT Join Consider all rows from the right table and common from both tables. ON keyword is used to specify the condition and join the tables. Query to get the loan_no, status and borrower date from two tables: – Let’s check the output of the above table after applying the right join on them. 3. INNER Join
A JOIN query is used to combine rows from two or more tables, based on a single column which can be used to store the same data from both tables. So we join over that point and join rows.
Inner Join = All common rows from both tables. While joining at least one column should be of the same data type and common among tables. Let us build a query to get the loan_no, status and borrower date from two tables: – Let’s check the output of the above table after applying the inner join on them.
As far as I understood your question, you can try this:
SELECT msg_id,
msg_body,
usersBy.userName AS msg_by,
usersTo.userName AS msg_to,
msg_by_usertype
FROM messages
INNER JOIN
(SELECT admin_id As id, admin_name as userName
FROM admin_table
UNION
SELECT manager_id As id, manager_name as userName
FROM manager_table ) usersTo ON msg_to = usersTo.id
INNER JOIN
(SELECT admin_id As id, admin_name as userName
FROM admin_table
UNION
SELECT manager_id As id, manager_name as userName
FROM manager_table ) usersBy ON msg_by = usersBy.id
Here is an SQL Fiddle to see how it works. (It only works if you cant have an admin who has the same id like a manager. Id should be unique in both tables.)
Please use the below SQL
SELECT msg_id,
msg_body,
usersBy.userName AS msg_by,
usersTo.userName AS msg_to,
msg_by_usertype
FROM messages
INNER JOIN
(SELECT admin_id As id, admin_name as userName,'admin' as usertype
FROM admin_table
UNION
SELECT manager_id As id, manager_name as userName,'manager' as usertype
FROM manager_table ) usersTo
ON msg_to = usersTo.id and msg_by_usertype = usersTo.usertype
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With