Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

mysql - how to get column name from 1 table to another table

I have two tables.

task table

task_name = primary key

+----+-----------+ 
| id | task_name |    
+----+-----------+ 
|  1 | Task 1    |    
|  2 | Task 2    |   
|  3 | Task 3    |    
+----+-----------+ 

manager table

task_id = foreign key of task_name in task table

+----+------------+---------+
| id | set_status | task_id |
+----+------------+---------+

I need to show the manager table, but instead of task_id (showing the IDs of the task_name ) I need to show the actual task name, in place of task_id. And also show set_status pending by defult when initialy task created by user.

required output

+----+------------+---------+
| id | set_status | task_id |
+----+------------+---------+
|  1 | pending    | Task 1  |
|  2 | pending    | Task 2  |
|  3 | pending    | Task 3  |
+----+------------+---------+

I have tried a few queries:

SELECT t1.task_name, t2.task_id
FROM task t1 
INNER JOIN manager t2
ON t1.id = t2.task_id

But I got

+-----------+---------+
| task_name | task_id |
+-----------+---------+
| Task1     |       1 |
+-----------+---------+
question from:https://stackoverflow.com/questions/66048603/how-to-get-column-name-from-1-table-to-another-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try using left join

SELECT t1.task_name, t2.task_id,'pending' as task_status
FROM task t1 
left JOIN manager t2
ON t1.id = t2.task_id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...