Please login or register.

Login with username, password and session length
Advanced search  

News:

We are constantly trying to improve PHP Freaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!

Pages: [1] 2  All

Author Topic: left Join problem  (Read 442 times)

0 Members and 1 Guest are viewing this topic.

proctk

  • Enthusiast
  • Offline Offline
  • Posts: 300
    • View Profile
left Join problem
« on: August 27, 2007, 10:20:21 PM »
HI

The below code is supposed to bring together multiple tables but for some reason its retuns repeat results for the query

based on my test data it should return DylanJennifer

but its returning DylanJenniferTylerJennifer

any ideas why

thank you
Code: [Select]
$query_get_Birthdays = ("SELECT * FROM users LEFT JOIN parent ON parent.owner_id = users.user_id LEFT JOIN children ON children.owner_id = users.user_id LEFT JOIN sibling ON sibling.owner_id = users.user_id WHERE month(childdob) = '3' OR month(spousedob) = '3' AND users.user_id = '$user_id' OR month(siblingdob) = '3' AND sibling.owner_id ='$user_id' OR month(parentdob) = '3' AND parent.owner_id = '$user_id'");

$get_birthdays = mysql_query($query_get_Birthdays) or die ("Invalid query". mysql_error());

while($birthdays = mysql_fetch_assoc($get_birthdays)){
echo $birthdays['childfirstname'];
echo $birthdays['spousefirstname'];
echo $birthdays['parentfirstname'];
echo $birthdays['siblingfirstname'];
}
Logged

btherl

  • Guru
  • Addict
  • *
  • Online Online
  • Posts: 2,769
  • Matt is the best!
    • View Profile
Re: left Join problem
« Reply #1 on: August 27, 2007, 11:49:46 PM »
Can you reformat your output a little?  I can't tell if you are getting more rows or if you are getting more columns than you expected.
Logged

spfoonnewb

  • Enthusiast
  • Offline Offline
  • Posts: 215
    • View Profile
Re: left Join problem
« Reply #2 on: August 28, 2007, 12:30:53 AM »
Meh too many join's for me.. Haven't used them in a while.. I did format it to take a look, it's probably one of those or's

Code: [Select]
<?php
$q 
mysql_query("SELECT * FROM `users` LEFT JOIN `parent` ON `parent.owner_id` = 'users.user_id' LEFT JOIN `children` ON `children.owner_id` = 'users.user_id' LEFT JOIN `sibling` ON `sibling.owner_id` = 'users.user_id' WHERE `month(childdob)` = '3' OR `month(spousedob)` = '3' AND `users.user_id` = '".$user_id."' OR `month(siblingdob)` = '3' AND `sibling.owner_id` ='".$user_id."' OR `month(parentdob)` = '3' AND `parent.owner_id` = '".$user_id."'") or die ("Invalid query"mysql_error());
while(
$b mysql_fetch_assoc($q)){
echo $b['childfirstname'].$b['spousefirstname'].$b['parentfirstname'].$b['siblingfirstname'];
}
?>
« Last Edit: August 28, 2007, 12:32:22 AM by spfoonnewb »
Logged

Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 15,096
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: left Join problem
« Reply #3 on: August 28, 2007, 07:02:06 AM »
When using OR condtions, use () to define the logic

eg  A AND B OR C

 is it

A AND (B OR C)

or is it

(A AND B) OR C

Also, the WHERE conditions below are superfluous as you are matching on the id
   AND sibling.owner_id ='$user_id'
   AND parent.owner_id = '$user_id'

Could be other problems but, as there is no indication of which tables all those xxxdob fields come from, I cannot be sure
« Last Edit: August 28, 2007, 07:04:30 AM by Barand »
Logged
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

proctk

  • Enthusiast
  • Offline Offline
  • Posts: 300
    • View Profile
Re: left Join problem
« Reply #4 on: August 28, 2007, 12:12:18 PM »
Thank you for the replies, I'm having a hard time working with where to place the () around the ORS

I'll explain the xxdob and what tables they are from


childdob is from the table childen
spousedob is from the table users
siblingdob is from the table sibling
parentdob is from the table parent.

my goal is to get all results where the month is = 4(April)
Logged

proctk

  • Enthusiast
  • Offline Offline
  • Posts: 300
    • View Profile
Re: left Join problem
« Reply #5 on: August 28, 2007, 12:13:42 PM »
the code looks like this with many edits since my last post

Code: [Select]
$query_get_Birthdays = ("SELECT * FROM users LEFT JOIN (parent, sibling, children) ON (parent.owner_id = users.user_id AND children.owner_id = users.user_id AND sibling.owner_id = users.user_id) WHERE month(children.childdob) = '3' OR month(users.spousedob) = '3' OR month(sibling.siblingdob) = '3' OR month(parent.parentdob) = '3' ");

its returning the same results over and over
Logged

Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 15,096
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: left Join problem
« Reply #6 on: August 28, 2007, 02:43:52 PM »
Is this any better?
Code: [Select]
$query_get_Birthdays = "SELECT * FROM users
            LEFT JOIN parent ON parent.owner_id = users.user_id AND month(parent.parentdob) = '3'
            LEFT JOIN sibling ON sibling.owner_id = users.user_id AND month(sibling.siblingdob) = '3'
            LEFT JOIN children ON children.owner_id = users.user_id AND month(children.childdob) = '3' 
            WHERE  (month(users.spousedob) = '3')
                OR (parent.owner_id IS NOT NULL)
                OR (sibling.owner_id IS NOT NULL)
                OR (children.owner_id IS NOT NULL)";

Change '3' to '4' if you want April
« Last Edit: August 28, 2007, 03:25:48 PM by Barand »
Logged
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

proctk

  • Enthusiast
  • Offline Offline
  • Posts: 300
    • View Profile
Re: left Join problem
« Reply #7 on: August 28, 2007, 04:24:43 PM »
thank you for the help it works excellent,  The April / March thing was a small typo in my post.

So I understand going forward and so I can write better mysql Statements from a formating stand point a line break show appear for each new mysql function is OR AND SELECT etc.

also is there a tutorial or a witting on when and where to place () around statements
Logged

Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 15,096
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: left Join problem
« Reply #8 on: August 28, 2007, 05:04:58 PM »
The line breaks are optional. I prefer to do it that way to make it easier to read. As for the (), certainly use them with the And/OR combinations as I mentioned earlier, apart form that it's an aid to readability again.

I would recommend using the join syntax that I used.

Even if you are just using normal JOIN it's better to have

SELECT x,y,z FROM a JOIN b ON a.id = b.id WHERE something
rather than
SELECT x,y,z FROM a,b  WHERE something AND a.id = b.id

as it defines the relationship structure separate from the selection criteria and IMO afain it's easier to understand what's going on.

Also, I'd normally use table aliases (sometimes they are necessary) as an aid to readability - the query is less cluttered. If I hadn't been copy/pasting your code I'd have written it like this
Code: [Select]
SELECT * FROM users u
LEFT JOIN parent p ON p.owner_id = u.user_id AND month(p.parentdob) = '3'
LEFT JOIN sibling s ON s.owner_id = u.user_id AND month(s.siblingdob) = '3'
LEFT JOIN children c ON c.owner_id = u.user_id AND month(c.childdob) = '3' 
WHERE  (month(u.spousedob) = '3')
    OR (p.owner_id IS NOT NULL)
    OR (s.owner_id IS NOT NULL)
    OR (c.owner_id IS NOT NULL)

One last thing - avoid SELECT * unless you really do need to pull every column. Specify the columns you need

SELECT u.name, u.spousedob, .... FROM ...

The less data you fetch the more efficient the query, and again, it let's the reader see what the query is really doing. (If you had a 2Mb blob image in each record you bee pulling those too in every record even though the birthdate query doesn't need them.)

Hope that jelps
Logged
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

proctk

  • Enthusiast
  • Offline Offline
  • Posts: 300
    • View Profile
Re: left Join problem
« Reply #9 on: August 28, 2007, 06:11:18 PM »
Thank you for taking time for the explanation. One last question why did you include this " IS NOT NULL" for each id



« Last Edit: August 28, 2007, 06:12:20 PM by proctk »
Logged

Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 15,096
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: left Join problem
« Reply #10 on: August 28, 2007, 06:27:43 PM »
When using

user LEFT JOIN parent

if no match is found in the parent table then all result values from the parent table are NULL. So if parent.id value IS NOT NULL a matching record was found.

You are looking for users where
spouse dob in March OR there was a match against any of the other tables with dob in March.

Also note, because data may or may nor be present in the RIGHT table (parent in the example) the condition
month(p.parentdob)=3 was moved to the join condition and not left in the WHERE clause. This was why I wanted to know which tables they were in when I first posted.

Logged
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

proctk

  • Enthusiast
  • Offline Offline
  • Posts: 300
    • View Profile
Re: left Join problem
« Reply #11 on: August 29, 2007, 11:20:33 PM »
I have run into a little snag

Her is a a query that I created with what I have learned from this post


the problem with this query is if no records are found 'mysql_query_row_num' will return a result of 8 based on data in my tables.  The same problem holds true for the query that was created with this form.  The interesting thing is if I echo a value for a row three should be no results nothing is returned but the mysql_query_row_num still retuns a value

any thoughts why this is happening
Code: [Select]
  <?php $query_get_buddyPhoto = ("SELECT * FROM buddylink b
 LEFT JOIN users u ON b.buddy_id = u.user_id
 LEFT JOIN image_files img ON  img.user_id = b.buddy_id AND album = 'Member'
 WHERE b.owner_id = '$id' ORDER BY b.added"
); 
$get_buddyPhoto mysql_query($query_get_buddyPhoto) or die ("Invalid query"mysql_error());

while($buddyPhotomysql_fetch_assoc($get_buddyPhoto)){
Logged

proctk

  • Enthusiast
  • Offline Offline
  • Posts: 300
    • View Profile
Re: left Join problem
« Reply #12 on: August 29, 2007, 11:21:32 PM »
I must add that if I set a limit in the query it messes things all up
Logged

Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 15,096
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: left Join problem
« Reply #13 on: August 30, 2007, 07:57:00 PM »
"mysql_query_row_num" not in my manual. Is it one of your own functions?
Logged
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

darkfreaks

  • Fanatic
  • Offline Offline
  • Gender: Male
  • Posts: 3,570
    • View Profile
    • WWW
Re: left Join problem
« Reply #14 on: August 30, 2007, 08:15:04 PM »
dont ya mean mysql_num_rows ?
Logged

PHP Freaks Forums

 
 
Pages: [1] 2  All
 

Page created in 0.076 seconds with 20 queries.