我目前有这些表。服务器:MariaDB
(我删除了一些列以使其更简单)
Person table:
id | username | password
-------------------------------------------------
1 | hamster | strongpw
2 | user2 | password2
Snippet table:
id | title | description
--------------------------------------------------------
1 | BestTitle | GoodDesc
2 | Test2 | Test2Desc
3 | Test3 | Test3Desc
Favourite table:
person_id | snippet_id
------------------------------------
1 | 1
我试图达到这个结果
人员ID = 1的测试
snippet.id | snippet.title | is_favourite (check favourite table)
--------------------------------------------------------
1 | BestTitle | true
2 | Test2 | false
3 | Test3 | false
人员ID = 2的测试
snippet.id | snippet.title | is_favourite (check favourite table)
--------------------------------------------------------
1 | BestTitle | false
2 | Test2 | false
3 | Test3 | false
我尝试使用IN,JOIN,嵌套查询来实现。
答案 0 :(得分:0)
这是您想要的吗?
select s.id, s.title,
(exists (select 1
from Favourite f
where f.snippet_id = s.id and f.person_id = ?
)
) as is_favourite
from snippet s;
?
是您感兴趣的人ID的占位符。