MDanz Posted August 31, 2011 Share Posted August 31, 2011 I'm doing a left join query for the table below and it is not getting the correct results. It should return the results peter, john and david because they have the same sid and the wildcard is peter. Instead it is returning carl, peter, john and david. What do i have to change to correct this? SELECT child.* FROM mytable child LEFT JOIN mytable parent on parent.sid=child.sid WHERE parent.name LIKE '%peter%' ORDER BY child.id ASC idsidnameparent 0nullmike[/td] [td]10rickmike 21carlrick 31peterrick 41johnrick 51davidrick Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 31, 2011 Share Posted August 31, 2011 Add the following - AND parent.name != child.name Quote Link to comment Share on other sites More sharing options...
MDanz Posted August 31, 2011 Author Share Posted August 31, 2011 i tried that and now it is removing peter from results. So the results are carl, john and david. SELECT child.* FROM mytable child LEFT JOIN mytable parent on parent.sid=child.sid WHERE parent.name LIKE '%peter%' AND parent.name != child.name ORDER BY child.id ASC Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 31, 2011 Share Posted August 31, 2011 Opps, I misread your problem to be that you didn't want the selected name to be included. Is the data you posted correct? Carl has the same sid as the others. There's nothing in what you posted that would exclude carl form the result set. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.