数据库有2个表:user和task
现在要把task表查询出来,并且把customer_id,creator_id,executor_id这3个id替换成对应的user表里面的user_name值
这个能通过一次多表查询查出来吗?
我现在能实现替换1个字段,但3个字段同时从同一个user表查出来替换应该怎么弄?
select
task_id,
(select user_name from user a1 where a1.user_id=customer_id) customer,
task_title,
task_detail,
(select user_name from user a2 where a2.user_id=creator_id) creator,
(select user_name from user a3 where a3.user_id=executor_id) executor,
from task b
按你的需求,是把这三个字段换成user_name的值,那这三个字段跟user_name之间是怎么关联的呢?
确定关联条件,然后连接3次做替换就可以
关键在于搞清楚这三个id和user的表怎么关联,一个set就可以更新三个字段。上面题目里面也没提到,没办法写