What i’ve got is these two tables:

One prefix can be used by unlimited number of links. I’m receiving a list if id‘s from click_links table as a paramater.
Now my goal is to select a list of prefixes and to mark those which are used by links in that list.
What I’ve tried: my best attempt was this:
select p.id, p.contents, c.id,
case
when (c.id in (544,545...))
then 1
else 0
end as isused
from prefixes p
join click_links c on c.prefix_id = p.id
but that’s not so good, ’cause there are duplicates of prefix if it’s used by many links.
How can I get a list of prefixes without duplicates and with a mark (flag if you wish) showing whether it’s ised by at least one of links in the list?
edit
ended with this query:
SELECT distinct(p.id), p.contents,
IF(c.id IS NOT NULL, 1, 0) AS isused
FROM prefixes p
LEFT JOIN (SELECT DISTINCT (prefix_id), id
FROM click_links WHERE id IN(544, 545...)
)c
ON c.prefix_id = p.id;
I think this is what you are looking for, try it: