Jump to content

another query


poe

Recommended Posts

databes tables:

 

#1 = user

id | name

1 | chris

2 | mike

3 | jim

 

 

#2 = banners

id | url | filename | user_id | zone_id

1 | sony.com | style.jpg | 1 | 1

2 | nike.com | swoosh.jpg | 1 | 2

3 | mazda.com | zoomzoom.jpg | 2 | 1

4 | microsoft.com | windows.jpg | 3 | 4

 

 

#3 = zones

id | description

1 | mainpage:top

2 | feature:side bar

3 | blog:bottom

4 | sponsored:listing

 

 

i want to list all zones, and get the data for the user.

ie. my query for user 1 is:

$qx = " SELECT z.id, z.description, b.id, b.url, b.filename, b.user_id, b.zone_id ";
$qx .= " FROM zones AS z ";

$qx .= " LEFT JOIN banners=b ON b.zone_id = z.id "; 

$qx .= " WHERE b.user_id = 1 ";

 

 

however this only lists the 2 zones that user 1 has a banner, i want it to list all for zones and where user 1 does not have a banner, just leave blank

Link to comment
Share on other sites

You can't have the b.user_id in the where clause and expect it to behave like a left join

$qx = "SELECT z.id, z.description, b.id, b.url, b.filename, b.user_id, b.zone_id 
FROM zones AS z
LEFT JOIN banners AS b ON z.id = b.zone_id 
    AND b.user_id = 1
WHERE 1 = 1 --just here so you see where";

Link to comment
Share on other sites

You can't have the b.user_id in the where clause and expect it to behave like a left join

Because the LEFT JOIN has the effect of "nulling out" the joined table -- in this case, banners -- so you won't be able to examine the column's value in the WHERE clause.

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.