frankie3
frankie3

Reputation: 89

MySQL - Show rows to some users and not to others depending on one thing

I have 10 users in a DB. Each user can post as many links as he wants. Each user can see all the links posted by the other users.

If one user clicks on a link, ex: google.com, he'll not see it again, but those users who haven't clicked that link, they can still click that link.

For this I have 2 tables (perhaps they are badly built, besides they are not related with foreign keys).

linksPosted

id   |   link   |   user
1        g.com      john
2        h.com      patrick
3        i.com      stephan
4        k.com      bart
....

clicksMade

id   |   link   |   user
1        g.com      jack
2        h.com      nick
...

So, there is a main page where every user can see the links posted by other users. Everytime a link is posted, this will be added to linksPosted table. OK.

Then, for example, Jack clicked g.com, so he'll not be able to click g.com again. BUT, as Nick hasn't clicked g.com, he'll be able to click it.

How can I show some rows to some users and not to other not in a SQL query?

I've tried this without results I want:

Select * from linksPosted Except Select * from ClicksMade
SELECT * FROM linksPosted WHERE id NOT IN(SELECT id FROM ClicksMade);
SELECT t1.* FROM linksPosted AS t1 LEFT OUTER JOIN ClicksMade AS t2 ON t1.word = t2.word AND 1.user = t2.user WHERE t2.id IS NULL

Thanks.

Upvotes: 1

Views: 90

Answers (1)

Sashi Kant
Sashi Kant

Reputation: 13465

Try this ::

Select lp.link
from 
linkposted lp
left join 
(Select cm.id from clickMade where userId= ?
) as tempComment
cm on (lp.id=tempComment.id)
where cm.id is null 

OR

    Select lp.link
    from 
    linkposted lp
where id not in
    (
     Select cm.id from clickMade where userId= ?
    )

Upvotes: 1

Related Questions