mysql - Selecting multiple values from same column with a single Select query -


so, i've got 2 tables: users , tasks;

users:

 user_id    username    password    first_name  last_name   isadmin 

tasks:

 task_id    name    description     assessment  assigned_user_id fk creator_id fk   created_on  last_modified   status 

what want replace assigned_user_id , creator_id first_name + last_name users table. execute following query:

select `task_id`, `description`,`assessment`,  (select concat(`first_name`, ' ',`last_name`) `tasks`.`assigned_user_id` = `users`.`user_id`) assigned_user,  (select concat(`first_name`, ' ',`last_name`) `tasks`.`creator_id`=`users`.`user_id`) creator_user, `created_on`,`last_modified`,`status` `tasks` left join `users` on tasks.assigned_user_id = users.user_id  or tasks.creator_id =  users.user_id task_id=2 

the problem returns 2 rows. 1 assigned_user_id null , filled creator_id , other other way around:

 task_id    description     assessment  assigned_user   creator_user    created_on  last_modified   status   2         sha sa pii li   24          null            petyo chuliuv   2016-07-22  2016-07-22       1   2         sha sa pii li   24          gosho toshov            null    2016-07-22  2016-07-22       1 

question is: how return single row assigned_user , creator_user filled , did wrong? in advance!

i tested on sql server , reproduced same issue can of help.

when did test 2 select concat statements using same user_id both times. issue seems not checking both ids @ once both ids @ separate times. if use example first uses petyo's id in both of select concat statements (only filling creator_user role other 1 becomes false) , uses gosho's id in both of select concat statements fills 1 field (the assigned_user field) , making other 1 null.

so need join users table again. 1 assigned, 1 create. this...

select `task_id`, `description`,`assessment`,  (select concat(`u1.first_name`, ' ',`u1.last_name`)) assigned_user,  (select concat(`u2.first_name`, ' ',`u2.last_name`)) creator_user, `created_on`,`last_modified`,`status` `tasks` left join `users` u1 on tasks.assigned_user_id = u1.user_id  left join `users` u2 on tasks.creator_id = u2.user_id task_id=2 

before had or. not @ 1 side, id, @ other one, id, use @ once. that. if current user_id looking happens 1 of 2 uses single user_id.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -