Forum Moderators: open

Message Too Old, No Replies

problem with joins and null values

         

kuper20

10:52 pm on Jun 23, 2008 (gmt 0)

10+ Year Member



Hi, I have a weird problem on one of the queries I want to do. I want to do a left join on two tables so that it pretty much just adds a column(s) to my player profile database with info on whether they did a certain activity. So now I have a bunch of people with a null value in this field and a few people with an id for the activity done.

I want to select everyone who either has a null value or doesn't have a certain number there, but when I try to do a != comparison in the where clause it only picks the people who have no null value, so the whole load of people with null values doesn't show up when I do a != comparison. How do I get around this?

Thanks

janharders

11:00 pm on Jun 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



erm, I'm not sure about this, did you try
WHERE table.field != 17 AND table.field IS NOT NULL
?
usually, I'd say that it shouldn't matter, but my sql is rusty.

if that doesn't do it, why don't you build a simple reproducable testcase (minimal create statements + select with joins) what we can test and work on?

kuper20

12:14 am on Jun 24, 2008 (gmt 0)

10+ Year Member



So I tried that and it still isn't working correctly. Here's a test case that simulates what I'm trying to do and what's going wrong.


//to make this script work just add the db "mydb" and change the host, user, and password
$host = "localhost";
$user = "root";
$pass = "";

$conn = @mysql_connect("$host",
"$user", "$pass");
if (!$conn) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}

// Select the player profile database
if (! @mysql_select_db("mydb") ) {
echo( "<P>Unable to locate the " .
"database at this time.</P>" );
exit();
}

mysql_query("CREATE TABLE `mydb`.`player_info` (
`pid` INT NOT NULL ,
`Name` TEXT NOT NULL ,
PRIMARY KEY ( `pid` )
)");

mysql_query("CREATE TABLE `mydb`.`playerActivities` (
`pid` INT NOT NULL ,
`aid` INT NOT NULL ,
PRIMARY KEY ( `pid` )
)");

mysql_query("INSERT INTO `mydb`.`player_info` (`pid` ,`Name`)
VALUES ('1', 'myName1'),
('2', 'myName2' ),
('3', 'myName3' )");

mysql_query("INSERT INTO `mydb`.`playerActivities` (`pid` ,`aid`)
VALUES ('1', '1'),
('2','2')");

//so player 1 has done activity 1 and
//player 2 has done activity 2 and
//player 3 has done nothing

//the following query should output player 2 and then player 3
//what it's doing now is only outputting player 2

$activityid = 1;

$sql = "SELECT * FROM player_info LEFT JOIN playerActivities ON player_info.pid=playerActivities.pid WHERE playerActivities.aid != $activityid AND playerActivities.aid IS NOT NULL";

$result = mysql_query( $sql );
while( $row = mysql_fetch_array( $result ) )
{
echo $row[Name];
echo "<br>";
}

topr8

12:22 am on Jun 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i've just skimmed this so could be well out ...but shouldn't it be:

>>WHERE playerActivities.aid != $activityid AND playerActivities.aid IS NOT NULL

WHERE playerActivities.aid != $activityid OR playerActivities.aid IS NULL

aren't you looking for a NULL value?

kuper20

12:46 am on Jun 24, 2008 (gmt 0)

10+ Year Member



haha, so I was being careless and I didn't even think about that code when I added it. You are completely right and now it works fine, that's all I needed :)