V
V
Vadim Leven2014-03-23 23:19:39
SQL
Vadim Leven, 2014-03-23 23:19:39

SQL: How to fetch data with missing field values?

There are two tables: users(fields: id, login, password, email) and users_online(fields: id_user, last_time).
I make a selection according to the following SQL query:

SELECT * 
FROM users, users_online
WHERE (
users.id = users_online.id_user
)

As a result, it gives me records of only those id that are in both tables.
I need to make a selection that will display all records from the users table and if there are records in the users_online table for a certain id, then supplement the first table with these values, if there are none, leave the field values ​​empty accordingly.
How should it be done? What SQL query should I write?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
litruk, 2014-03-23
@levenwadim

SELECT * FROM users LEFT JOIN users_online ON(users.id = users_online.id_user);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question