Postgresql与其他用户的相似之处
我有三张桌子: 用户, 项目。 喜欢 考虑到另一个用户的现有喜好,如何发出属于用户的项目的请求 我需要结果:Postgresql与其他用户的相似之处,postgresql,Postgresql,我有三张桌子: 用户, 项目。 喜欢 考虑到另一个用户的现有喜好,如何发出属于用户的项目的请求 我需要结果: item_id owner_id its_like 1 it123 us123 us789 2 it456 us123 3 it789 us123 us789 谢谢。使用左连接。将其他用户id置于联接条件中: select i.item_id, i.owner_id, l.user_id as its_like from t_items i
item_id owner_id its_like
1 it123 us123 us789
2 it456 us123
3 it789 us123 us789
谢谢。使用左连接。将其他用户id置于联接条件中:
select i.item_id, i.owner_id, l.user_id as its_like
from t_items i
left join t_likes l
on i.item_id = l.item_id and l.user_id = 'us789'
where owner_id = 'us123';
item_id | owner_id | its_like
---------+----------+----------
it123 | us123 | us789
it456 | us123 |
it789 | us123 | us789
(3 rows)
select i.item_id, i.owner_id, l.user_id as its_like
from t_items i
left join t_likes l
on i.item_id = l.item_id and l.user_id = 'us789'
where owner_id = 'us123';
item_id | owner_id | its_like
---------+----------+----------
it123 | us123 | us789
it456 | us123 |
it789 | us123 | us789
(3 rows)