By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table users( ID int )
insert into users values(1);
insert into users values(2);
create table book_buyed( ID int, user_id int, volume int )
insert into book_buyed values(12,1,200);
create table book_request( ID int,id_cliente int, time_request datetime )
insert into book_request values(1,2,now());
-- # Original SQL
select
U.id AS ID,
B.time_request AS Time,
(select SUM(volume) from book_buyed O where O.user_id = U.id) as BookBuyed
from book_request B
inner join users U on B.id_cliente = U.id
ID | Time | BookBuyed |
---|---|---|
2 | 2019-09-16 11:48:16 | null |
-- # SQL in the answer
select
U.id AS ID,
B.time_request AS Time,
sum(volume) as BookBuyed
from users U
left join book_request B on B.id_cliente = U.id
left join book_buyed O on O.user_id = U.id
group by U.id, B.time_request
ID | Time | BookBuyed |
---|---|---|
1 | null | 200 |
2 | 2019-09-16 11:48:16 | null |