D
D
direx2014-12-07 15:26:54
MySQL
direx, 2014-12-07 15:26:54

Database query with JOIN from multiple tables?

Good afternoon.
Interested in such a question, or rather how to correctly compose a mysql query.
There are several tables, here is their structure:
USER:

id | login | 
1 | Николай |
2 | Дмитрий |

TASK:
id | author_user_id | performer_user_id | task_message | status
1 | 1 | 2 | Задача 1 | 2

TASK_STATUS:
id | status
1 | Ожидает

How to compose a correct query so that mysql returns to me:
id : 1
author_user_id : {login: Николай}
performer_user_id: {login: Дмитрий}
task_message: Задача 1
status: Ожидает

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2014-12-07
@Rsa97

SELECT `t`.`id`, `au`.`login`, `pu`.`login`, `t`.`task_message`, `ts`.`status`
    FROM `TASK` AS `t`
    LEFT JOIN `USER` AS `au` ON `au`.`id` = `t`.`author_user_id`
    LEFT JOIN `USER` AS `pu` ON `pu`.`id` = `t`.`performer_user_id`
    LEFT JOIN `TASK_STATUS AS `ts` ON `ts`.`id` = `t`.`status`
    WHERE ...

E
Eugene, 2014-12-07
@Nc_Soft

Make 3 requests, it will be better.
And it's not because I don't know how a join works, but because joins are evil.

Y
ylebedev, 2014-12-07
@ylebedev

If it's not difficult, throw off the SQL file (exact export of tables).
Too lazy to fill your base with your hands.
DBForge to graph. form allows you to design a database by issuing the correct version of the query.
Including prevents confusion in JOIN
SELECT
task.id,
task.message,
task_status.title,
user_profile.firstName,
user_profile.lastName,
user_profile_1.firstName AS performer_user_id
FROM task
LEFT JOIN task_status
ON task.status = task_status.id
LEFT JOIN user_profile
ON task .author_user_id = user_profile.user_id
LEFT JOIN user_profile user_profile_1
ON task.performer_user_id = user_profile_1.user_id
1
admin
adminovich
pertovich
About the sea miya gadzhubas Waiting
Added
a couple of columns
For SQL, it's better to use 1 long query than 1000 small loops.
I have already suffered from cycles in the old school php. As a result, php bent SQL at 50,000 requests per second.
because the product has grown.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question