March 13, 2012

mySQL – WHERE, AND/OR selecting

Question by Kolind

I’m trying to select all posts from database, (where an ID is = an id from another table, which connects the people i’m in network with) OR (to select posts where i’m the writer of the post.). Actually i’m trying to get posts from people in my network AND my own.

I’ve found out how to do this, but when using the OR clause it’s selecting the rows twice. So for example, if I write a post it’s duplicated when echo’ed out from database.

My mySQL:

$sId = validate::intValidate($_SESSION['userId']);
$sql = "SELECT * FROM networkPost
            INNER JOIN authentication
                ON networkPost.FK_networkYou=authentication.userId
            INNER JOIN network
                ON networkPost.FK_networkYou=network.networkYou
            WHERE networkMe=$sId AND FK_networkYou=networkYou OR networkYou=$sId
            ORDER BY networkPostId DESC";

networkYou is: The userId of the post writer.

So how do I select my own posts and my networks posts?

Answer by Starx

You have few errors in your code

validate::intValidate($_SESSION['userId']); //missing square bracket

And the WHERE part of the query is missing brackets

WHERE (networkMe=$sId AND FK_networkYou=networkYou) OR networkYou=$sId

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!