Jump to content


Mysql LEFT JOIN help, please

  • Please log in to reply
1 reply to this topic

#1 kid_drew

  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 11 September 2006 - 02:10 PM

Hey guys,
I'm having a problem with a left join statement.  When I query the database from the phpmyadmin console, it works fine, but I'm having problems with the mysql_query call in php.  The query is:

"select * from users left join images on users.username = images.username AND images.list_order = 0"

This is supposed to grab all users whether they have images or not, but if they do, the images fields should be populated instead of NULL.  if the user has an image listed in the "images" table, everything works fine.  The problem is that if the user does not have an image listed in table "images", then the username field returns NULL.  Again, the query looks like it works perfectly if I do it from the phpmyadmin console, but not in PHP.

Any ideas?

#2 shoz

  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 11 September 2006 - 02:28 PM

The images.username field is overriding the user.username field. A quick fix would be the following
u.username AS uname, u.*, i.*
users AS u
u.username = i.username
AND i.list_order = 0
You'd now access the username in the column uname rather than username. You can also list all the columns you'd like from each table (users.column1, images.column1 ...) instead of using "*" to avoid the problem.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users