There is a table of users. There is a table with the data of these users. Tell me how to extract data from the user table and from the user data table in one query? I am writing in Java (JavaEE). Moreover, these tables have their own id - autoincrement. In the table with data there is a link to the user id to which this data belongs. How to pull this data on the link
|
1 answer
Usually, in such a case, a multi-query query is used, which is organized using the keyword JOIN , for example
SELECT u.*, d.* FROM users AS u LEFT JOIN users_data AS d ON u.id = d.user_id Those. you have a users table and a users_data data table with a users_data foreign key user_id user id . In the FROM keyword, you use a JOIN or LEFT JOIN define a link, and the link condition is specified in the ON condition.
- What does
AS umean? - Tsyklop - one@Tsyklop This is an alias to make the table names shorter and not to write users and users_data every time. Instead of users.id = users_data.user_id, you can write u.id = d.user_id. - cheops
- but. got it thank you - Tsyklop
|
SELECT u.col1, u.col2, u.col3, ud.col1, ud.col2, ud.col3 FROM users u LEFT JOIN user_data ud ON ud.user_id = u.id- Alexey Shimansky