Answer the question
In order to leave comments, you need to log in
How to make a selection from 3 tables?
There are three tables with the following content:
mysql> select * from `users`;
+----+-------+----------+----------+
| id | email | password | nickname |
+----+-------+----------+----------+
| 1 | email | password | nickname |
+----+-------+----------+----------+
| 2 | email | password | nickname |
+----+-------+----------+----------+
mysql> select * from `users_data`;
+---------+-----------+----------+
| user_id | firstname | lastname |
+---------+-----------+----------+
| 1 | firstname | lastname |
+---------+-----------+----------+
| 2 | firstname | lastname |
+---------+-----------+----------+
mysql> select * from `users_sessions`;
+---------+----------------------------------+
| user_id | token |
+---------+----------------------------------+
| 1 | 01234567890123456789012345678901 |
+---------+----------------------------------+
| 2 | 01234567890123456789012345678902 |
+---------+----------------------------------+
array
'id' => 1
'email' => 'email'
'password' => 'password'
'nickname' => 'nickname'
'data' =>
array
'user_id' => 1
'firstname' => 'firstname'
'lastname' => 'lastname'
'session' =>
array
'user_id' => 1
'token' => '01234567890123456789012345678901'
<?php
$result['id'];
$result['nickname'];
$result['data']['firstname'];
$result['data']['lastname'];
$result['session']['user_id'];
$result['session']['token'];
select * from
(select * from `users_sessions` where `user_id` = 1 and `token` = '01234567890123456789012345678901') as `session`,
(select * from `users_data` where `user_id` = `session`.`id`) as `data`,
select * from `users` where `id` = `session`.`id`
Answer the question
In order to leave comments, you need to log in
If users, users_data and users_sessions are related one-to-one, then left join (although why not store everything in one place in this case).
If one-to-many, then on request for each table and process in code, there is no other way.
SELECT something FROM users as u
LEFT JOIN users_data as ud ON u.id = ud.user_id
LEFT JOIN users_sessions as us ON u.id = us.user_id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question