There is a query to the database, to a single table using pdo

SELECT id, name, lastname, login, birthday, main_photo FROM users WHERE id = :id 

How to modify the query so that the sample was from three different tables? I do as follows:

 SELECT id, name, lastname, login, birthday, main_photo FROM users, regals, user_info WHERE id = :id 

thank

  • You write the conditions for the gluing of the tables, without the gluing conditions they multiply and decide why it is needed at all. maybe you do not need gluing but you need a union. so it’s better to write the structure of the tables in vporse, which records approximately in them and what the output should give the request - Mike

2 answers 2

 SELECT u.id, u.name, u.lastname, u.login, u.birhday, u.main_photo, r.id, ui.id FROM users AS u INNER JOIN regals AS r ON r.id = u.id INNER JOIN user_info AS ui ON ui.id = u.id WHERE u.id = id пользователя. 

However, this is on condition that you have links between the tables. Or simply use your SELECT for each table, and put a UNION between them to combine the samples as a result.

    Emm, so it is from the three tables. Or are you talking about join?

     SELECT * FROM users u JOIN regals r ON r.id = u.id JOIN user_info ui ON ui.id = u.id WHERE u.id = :id 
    • In fact, I have no idea what a join is and what a union is. And from the users table, in addition to the ID, other fields are selected, and from the other two tables, only the fields with the ID are alexandr.patrachi
    • @ alexandr.patrachi what is selected is written after the word select, in this case it is * - i.e. all fields from all tables. and the join conditions say what conditions the tables join. those. what records from regals for example to choose to a specific record from users. the words join could be replaced by commas (as you originally did) and write join conditions in where. And your request is not complicated, just to write it is impossible without understanding what exactly you want to receive. He will learn how to write requests of this level in 1 day. - Mike
    • What I want to get, I understand. There are three tables users, regals, user_info, I want to select from the users table the id, name, lastname, login, birthday, main_photo fields from the users table and the alexandr.patrachi fields from the regals and user_info tables