views:

1142

answers:

2

I have three tables called: users, facilities, and staff_facilities.

users contains average user data, the most important fields in my case being users.id, users.first, and users.last.

facilities also contains a fair amount of data, but none of it is necessarily pertinent to this example except facilities.id.

staff_facilties consists of staff_facilities.id (int,auto_inc,NOT NULL),staff_facilities.users_id (int,NOT NULL), and staff_faciltities.facilities_id (int,NOT NULL). (That's a mouthful!)

staff_facilities references the ids for the other two tables, and we are calling this table to look up users' facilities and facilities' users.

This is my select query in PHP:

SELECT users.id, users.first, users.last FROM staff_facilities LEFT JOIN users ON staff_facilities.users_id=users.id WHERE staff_facilities.facilties_id=$id ORDER BY users.last

This query works great on our development server, but when I drop it into the client's production environment often times blank rows appear in the results set. Our development server is using the replicated tables and data that already exist on the client's production server, but the hardware and software vary quite a bit.

These rows are devoid of any information, including the three id fields that require NOT NULL values to be entered into the database. Running the query through the MySQL management tools on the backend returns the same results. Searching the table for NULL fields has not turned up anything.

The other strange thing is that the number of empty rows is changing based on the varying results caused by the WHERE clause id check. It's usually around one to three empty rows, but they are consistent when using the same parameter.

I've many times dealt with the returning of nearly duplicate rows due to LEFT JOINS, but I've never had this happen before. As far as displaying the information goes, I can easily hide it from the end user. My concern is primarily that this problem will be compounded as time passes and the number of records grows larger. As it sits, this system has just been installed, and we already have 2000+ records in the staff_facilities table.

Any insight or direction would be appreciated. I can provide further more detailed examples and information as well.

A: 

Have you run the queries via MySQL Query Browser on the production machine?

p.campbell
Unforunately, I am not able to do so. I have no physical or even remote command line access to the machine. I can only use a very limited back end(It does not even have phpmyadmin installed, but instead a custom system).
Mesidin
+2  A: 

You are only selecting columns from the table on the right side of the join. Of course some of them are completely null, you did a left join. So those records match to an id in the table on the left side of the join but not to any data on the right side of the join. Since you aren't returning any columns from the left table, you see no data.

HLGEM
Oh, my word. *Slaps forehead* I've been staring at this too long. That's totally it. Simple and obvious. It helps to have a fresh set of eyes. Thanks a ton!
Mesidin
Hey I can't tell you how often I have stared at something trying to figure out what was wrong and someone else spotted it in a second when I showed it to them. We've all been there I think.
HLGEM