How to join table based on two column in mysql?

Prajna Hegde

I have two tables as mentioned below.

user table
id  | username  | password  | status   |
1   | Prajna    | *****     | active   |
2   | Akshata   | *****     | active   |
3   | Sanjana   | *****     | inactive |

test table
id  | project_name   | created_by (user id) | edited_by (user id)  |
1   | Test           | 1                    | 2                    |
2   | Trial          | 1                    | 1                    |
3   | Pro1           | 2                    | 2                    |

I am trying with below query.

select project_name, user.username from test join user on user.id=test.created_by where user.status='active';

I wanted the result like below

I want to retrieve the result as below How can I retrieve?

project_name   | username(created by) | username (edited by) |
Test           | Prajna               | Akshata              |
Trial          | Prajna               | Prajna               |
Pro1           | Akshata              | Akshata              |
alimbaronia

Try this code.

create table `user`
(
  `id` int,
  `username` varchar(20),
  `password` varchar(20),
  `status` varchar(20)
)
insert into `user` (`id`,`username`,`password`,`status`) values
(1,   'Prajna',    '*****',     'active'),
(2,   'Akshata',   '*****',     'active'),
(3,   'Sanjana',   '*****',     'inactive')
create table `test`
(
  `id` int,
  `project_name` varchar(20),
  `created_by` int,
  `edited_by` int
)
insert into `test` (`id`,`project_name`,`created_by`,`edited_by`) values
(1,   'Test',   1,     2),
(2,   'Trial',  1,     1),
(3,   'Pro1',   2,     2)
SELECT
  `t`.`project_name`, 
  `ua`.`username` as 'username (created by)' , 
  `ub`.`username` as 'username (edited by)' 
FROM `test` `t` 
  JOIN `user` `ua` ON `t`.`created_by` = `ua`.`id` 
  JOIN `user` `ub` ON `t`.`edited_by` = `ub`.`id`
WHERE 
 `ua`.`status` = 'active' 
  AND `ub`.`status` = 'active'
order by `t`.`id`
project_name | username (created by) | username (edited by)
:----------- | :-------------------- | :-------------------
Test         | Prajna                | Akshata             
Trial        | Prajna                | Prajna              
Pro1         | Akshata               | Akshata             

db<>fiddle here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to join table with prefix column to another two table in PHP MySQL or Laravel

From Dev

Mysql Query Join two table Order by one table column

From Dev

In MySQL how to join tables using their names which are based on column values of another table?

From Dev

MySQL Join two table

From Dev

How to join the table based on main table column value in SQL Server?

From Java

mysql: join two tables, and after split a column of the result table

From Dev

MySQL multiple count based on two column with multiple GROUP BY in single table

From Dev

MySQL - JOIN based on value of column?

From Dev

How to select rows based on join on two columns of same table

From Dev

How to select rows based on join on two columns of same table

From Dev

How to join two tables with one similar column into one table

From Dev

MySQL join two table query

From Dev

mysql left join with two table

From Dev

MySQL join two table query

From Dev

mysql: two table join with sum

From Dev

how can use two where in one column with mysql join query?

From Dev

How to Join Two Table In mysql database and Fetch Records.?

From Dev

How to join two table with different value using Mysql

From Dev

How to migrate IDs from JOIN table into foreign key column in MySQL

From Dev

MYSQL join two different columns from two different table as single column

From Dev

mysql join two table rows in one table

From Dev

Codeigniter join two table based on two join conditions

From Dev

Inner Join table based on column value

From Dev

SQL Adding a Column to table, based on a inner join

From Dev

mysql update column self join based on two reference columns (kind of vlookup)

From Dev

MYSQL: How to insert rows in a table based on a condition of other two tables

From Dev

How to write mysql query to achieve the following type of join (Join based on value of a column )

From Dev

Query for how to join two tables with sum oparation in one table condition based on another table?

From Dev

how to use join to join two table in sails

Related Related

  1. 1

    How to join table with prefix column to another two table in PHP MySQL or Laravel

  2. 2

    Mysql Query Join two table Order by one table column

  3. 3

    In MySQL how to join tables using their names which are based on column values of another table?

  4. 4

    MySQL Join two table

  5. 5

    How to join the table based on main table column value in SQL Server?

  6. 6

    mysql: join two tables, and after split a column of the result table

  7. 7

    MySQL multiple count based on two column with multiple GROUP BY in single table

  8. 8

    MySQL - JOIN based on value of column?

  9. 9

    How to select rows based on join on two columns of same table

  10. 10

    How to select rows based on join on two columns of same table

  11. 11

    How to join two tables with one similar column into one table

  12. 12

    MySQL join two table query

  13. 13

    mysql left join with two table

  14. 14

    MySQL join two table query

  15. 15

    mysql: two table join with sum

  16. 16

    how can use two where in one column with mysql join query?

  17. 17

    How to Join Two Table In mysql database and Fetch Records.?

  18. 18

    How to join two table with different value using Mysql

  19. 19

    How to migrate IDs from JOIN table into foreign key column in MySQL

  20. 20

    MYSQL join two different columns from two different table as single column

  21. 21

    mysql join two table rows in one table

  22. 22

    Codeigniter join two table based on two join conditions

  23. 23

    Inner Join table based on column value

  24. 24

    SQL Adding a Column to table, based on a inner join

  25. 25

    mysql update column self join based on two reference columns (kind of vlookup)

  26. 26

    MYSQL: How to insert rows in a table based on a condition of other two tables

  27. 27

    How to write mysql query to achieve the following type of join (Join based on value of a column )

  28. 28

    Query for how to join two tables with sum oparation in one table condition based on another table?

  29. 29

    how to use join to join two table in sails

HotTag

Archive