Jump to content

excluding data from sql output if..


Jaynesh

Recommended Posts

Hello

 

I have two tables

 

post(post_id, user_id, post)

ignore(post_id, user_id)

 

I have set it up so it outputs all the contents within the 'post' column.

How would I stop it from outputting all the posts that are in the ignore table?

 

 

Link to comment
Share on other sites

SELECT * FROM post WHERE post_id NOT IN (SELECT post_id FROM ignore)

though if user_id in the ignore table is the user who ignored the post, you would have something like this... assuming the user id is stored in the session

SELECT * FROM post WHERE post_id NOT IN (SELECT post_id FROM ignore WHERE user_id='{$_SESSION['user_id']}')

Link to comment
Share on other sites

Hi.

 

It doesn't seem to be working.

 

My code is a little more complicated, I'm not sure I'm implementing it correctly.

I've created a friends system, whenever a friend creates a post it will be displayed to his/her friends.

This is the query that displays it to them. I want to filter out any posts that are ignored.

 

SELECT Users.id, Posts.post, Users.username, Posts.post_id
FROM Posts, Users, Friends
WHERE Posts.username_id = Users.id 
AND Friends.user_id = $user 
AND Friends.friend_id = Posts.username_id
NOT IN (SELECT Posts.post_id FROM ignore)

Link to comment
Share on other sites

you have to select post_id from ignore, not posts.post_id, as they are seperate queries...

SELECT Users.id, Posts.post, Users.username, Posts.post_id
FROM Posts, Users, Friends
WHERE Posts.username_id = Users.id 
AND Friends.user_id = $user 
AND Friends.friend_id = Posts.username_id
NOT IN (SELECT post_id FROM ignore)

 

.. though that will return the post_id of every ignored post, by any user. you need to change the last select to incorporate the user's id, i.e.

SELECT Users.id, Posts.post, Users.username, Posts.post_id
FROM Posts, Users, Friends
WHERE Posts.username_id = Users.id 
AND Friends.user_id = $user 
AND Friends.friend_id = Posts.username_id
NOT IN (SELECT post_id FROM ignore WHERE user_id=$user)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.