Query 2 SQL tables bring all the results

Asked

Viewed 100 times

1

I’m doing an SQL query to bring users, but I consult 2 tables: users and users_vip

No users have all users and users_vip only those who are vip (active or not).

When I make my query joining the 2 tables it only brings who has registration in vip, because I use the WHERE u.id = uv.iduser, the query is like this:

SELECT uv.status, u.id, u.name, u.username, u.email, uv.datapedido, uv.dataconfirm, uv.datafinal
FROM euk_users u, euk_users_vip uv
WHERE u.id = uv.iduser

If I have 10 common users and 1 VIP, the query will only bring this 1 VIP.

I wanted that when consulting it also bring users who are in the users but are not in users_vip, with the data missing blank, in zero, or something like that. How can I do that?

1 answer

3


You can do this using the LEFT JOIN, that will return you everything you have in the right table and what you have in the left table for example:

SELECT 
    uv.status, 
    u.id, u.name, 
    u.username, 
    u.email, 
    uv.datapedido, 
    uv.dataconfirm, 
    uv.datafinal
FROM euk_users u
LEFT JOIN euk_users_vip uv
ON u.id = uv.iduser;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.