Monkuar Posted January 15, 2012 Share Posted January 15, 2012 all i am doing is trying to left join my column row "to_id" and "from_id" i can successfully left join to_id but once i try to add "from_id" to the "ON" clause it makes all my data blank SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON to_id = ibf_members.id WHERE to_id = 1 That query works fine, but when i try to add my "from_id" column the query works with no error's but no results are shown SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON (to_id = ibf_members.id AND from_id = ibf_members.id) WHERE to_id = 1 Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/ Share on other sites More sharing options...
AyKay47 Posted January 15, 2012 Share Posted January 15, 2012 Then your conditions are not being met, how can ibf_members.id equal two values at once? Perhaps you meant OR not AND Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1307999 Share on other sites More sharing options...
blacknight Posted January 16, 2012 Share Posted January 16, 2012 once you left join you have to declare each table each field is from im guessing form_id and to_id are in gold_logs SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON (gold_logs.to_id = ibf_members.id AND gold_logs.from_id = ibf_members.id) WHERE gold_logs.to_id = 1 and see if that works.. Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308001 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 once you left join you have to declare each table each field is from im guessing form_id and to_id are in gold_logs SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON (gold_logs.to_id = ibf_members.id AND gold_logs.from_id = ibf_members.id) WHERE gold_logs.to_id = 1 and see if that works.. This does the same thing as before, shows no information? The problem is to_id = 1 from_id = 5 So that means user with a id = 5 sent 200 Gold to to_id 1 (user id 1), Now I just need to left join both those columns (from_id/to_id) on ibf_members, so I can extract the name of EACH user. Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308005 Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 once you left join you have to declare each table each field is from im guessing form_id and to_id are in gold_logs SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON (gold_logs.to_id = ibf_members.id AND gold_logs.from_id = ibf_members.id) WHERE gold_logs.to_id = 1 While this is good practice, it is only for ambiguity purposes, and this case in particular it will not affect the outcome. OP, simply join the table using the from_id. The where clause will control your main table. SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON gold_logs.from_id = ibf_members.id WHERE gold_logs.to_id = 1 Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308006 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 once you left join you have to declare each table each field is from im guessing form_id and to_id are in gold_logs SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON (gold_logs.to_id = ibf_members.id AND gold_logs.from_id = ibf_members.id) WHERE gold_logs.to_id = 1 While this is good practice, it is only for ambiguity purposes, and this case in particular it will not affect the outcome. OP, simply join the table using the from_id. The where clause will control your main table. SELECT gold_logs.*,name,star FROM gold_logs LEFT JOIN ibf_members ON gold_logs.from_id = ibf_members.id WHERE gold_logs.to_id = 1 1 Problem Using your query my results come out: Problem is, the name should not be the "the doctor" because it's not id 1, any idea on the fix? it should be the name of the whoever ahs user ID 1 in ibf_members Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308007 Share on other sites More sharing options...
blacknight Posted January 16, 2012 Share Posted January 16, 2012 opps yea i see that now too some one shouldent be the sender and the reciver at the same time which is what the on statement is stating if you use ON (gold_logs.to_id = ibf_members.id OR gold_logs.from_id = ibf_members.id) it will list both sent and recived Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308008 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 opps yea i see that now too some one shouldent be the sender and the reciver at the same time which is what the on statement is stating if you use ON (gold_logs.to_id = ibf_members.id OR gold_logs.from_id = ibf_members.id) it will list both sent and recived wont work because of the where id = 1 and it would show 3 rows instead of my 2 i just need to left join my column to_id and from_id to the column ID from ibf_members iuno this is so hard, i must need to read more mysql @_@ Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308009 Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 show both of your mysql tables, it may be that ibf_members needs to be the left table Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308013 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 show both of your mysql tables, it may be that ibf_members needs to be the left table my ibf_members only has 3 columns id name star should i re do my query but use ibf_members as the main 1 and LEFT JOIN the gold_logs? try that? i am really stumped it's almost working, it SHOULD work when i use the ON clause cause essentially i just want it to pull the name off ibf_members to correspond to each to_id and from_id.... Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308015 Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 SELECT gold_logs.*,members1.name,members1.star,members2.name,members2.star FROM gold_logs LEFT JOIN ibf_members members1 ON ibf_members.id = gold_logs.to_id LEFT JOIN ibf_members members2 ON ibf_members.id = gold_logs.from_id WHERE gold_logs.to_id = 1 Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308016 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 SELECT gold_logs.*,ibf_members.name,ibf_members.star FROM gold_logs LEFT JOIN ibf_members ON ibf_members.id = gold_logs.to_id LEFT JOIN ibf_members ON ibf_members.id = gold_logs.from_id WHERE gold_logs.to_id = 1 I get mySQL error: Not unique table/alias: 'ibf_members' Seems fishy why would it think it's not unique? Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308018 Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 yeah, refer to my edit Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308019 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 yeah, refer to my edit holy monkeys mySQL error: Unknown column 'ibf_members.id' in 'on clause' This is weird, man sorry for the trouble Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308021 Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 not your fault, I continuously keep messing up the query, its been a long day.. refer to my edit again in a minute Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308023 Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 never mind, i can't edit the post anymore, here SELECT gold_logs.*,members1.name,members1.star,members2.name,members2.star FROM gold_logs LEFT JOIN ibf_members members1 ON members1.id = gold_logs.to_id LEFT JOIN ibf_members members2 ON members2.id = gold_logs.from_id WHERE gold_logs.to_id = 1 Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308024 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 never mind, i can't edit the post anymore, here SELECT gold_logs.*,members1.name,members1.star,members2.name,members2.star FROM gold_logs LEFT JOIN ibf_members members1 ON members1.id = gold_logs.to_id LEFT JOIN ibf_members members2 ON members2.id = gold_logs.from_id WHERE gold_logs.to_id = 1 Hey, it's working now but I don't want to walk away from this seeming like I am just asking for help just "cuz" I want to actually learn this stuff. Which your query helped me greatly. now the problem is, how do I echo that out so I can differentiate between my to_id and from_id's ? This is how i echo it out in php <a href=?i={$r['to_id']}>{$r['name']}</a> That will show me the name and link them to there to_id, (which is there profile) Then for from: <a href=?i={$r['from_id']}>{$r['name']}</a> it's the same thing, but it's showing up weird here: See it's almost working now! But that to_id should have the username "Newman" hence that is id #1 from ibf_members, but it shows the id from 25 instead which shows the user id of 25's name which is "theDoctor" sorry this is really prob pissing you off, but this stuff has been bothering me the past 20hours and I have no where else to go Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308029 Share on other sites More sharing options...
Monkuar Posted January 16, 2012 Author Share Posted January 16, 2012 WOW I FIXED IT DUDe FINALLY members1.name as to_name,members1.star,members2.name as from_name WOW TOPIC RESOLVED DUDE THANK YOU SO MUCH :Dddddddddddddddddddddddd I guess I just needed to add the "AS ETC" THANKS SO MUCH BRO Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308031 Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 WOW I FIXED IT DUDe FINALLY members1.name as to_name,members1.star,members2.name as from_name WOW TOPIC RESOLVED DUDE THANK YOU SO MUCH :Dddddddddddddddddddddddd I guess I just needed to add the "AS ETC" THANKS SO MUCH BRO no problem, im glad you figured that last part out on your own. good work Quote Link to comment https://forums.phpfreaks.com/topic/255098-stupid-mysql-query/#findComment-1308035 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.