thedepotnetwork Posted December 31, 2009 Share Posted December 31, 2009 I am trying to do the following: I have two tables, one contains all the vendors "vendors" the other contains the vendors service areas "vendors_service_areas" I am attempting to only get the records from the "vendors_services_areas" table where the "vendor_state" in the "vendors" table is equal to 46 (utah). Both tables have the column, "vendor_id" Here is what I had, but didn't work: SELECT vca.vendor_id, vca.service_area, vca.service_type, vca.post_data FROM vendors_service_areas AS vca, vendors AS v WHERE v.vendor_state =46 AND v.vendor_id = vca.vendor_id PLease Help! Still learning. Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/ Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 It looks OK in general, although it could be rewritten as: SELECT vca.vendor_id, vca.service_area, vca.service_type, vca.post_data FROM vendors_service_areas AS vca CROSS JOIN vendors AS v USING(vendor_id) WHERE v.vendor_state =46 But that's not really different. What do you mean by 'it didn't work'. Did it throw any errors, or did it not return expected results? Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986586 Share on other sites More sharing options...
thedepotnetwork Posted December 31, 2009 Author Share Posted December 31, 2009 Thanks for the help. What I mean is it didn't get all the results? I did a test, logged into our backend, and found that it missed many service areas... so something isn't right... DIDN"T RETURN ALL EXPECTED RESULTS Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986588 Share on other sites More sharing options...
thedepotnetwork Posted December 31, 2009 Author Share Posted December 31, 2009 Hey! Yours worked! wow, thank you! SELECT vca.vendor_id, vca.service_area, vca.service_type, vca.post_data FROM vendors_service_areas AS vca CROSS JOIN vendors AS v USING ( vendor_id ) WHERE v.vendor_state =46 Your SQL query has been executed successfully Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986590 Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 That's a bit strange, cause in general they're equivalent to each other. Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986591 Share on other sites More sharing options...
thedepotnetwork Posted December 31, 2009 Author Share Posted December 31, 2009 Yes, I thought it a bit strange... but thanks for helping me brush up on skills. Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986592 Share on other sites More sharing options...
thedepotnetwork Posted December 31, 2009 Author Share Posted December 31, 2009 One more for you... can't seem to get it: similar thing, but in this case, different fields names... SELECT * FROM voting AS vt CROSS JOIN vendors AS v USING ( vt.item_id, v.vendor_id ) WHERE v.vendor_state =46 Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986594 Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 USING ( item_id, vendor_id ) And you can use USING only if the columns exist in both tables Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986595 Share on other sites More sharing options...
thedepotnetwork Posted December 31, 2009 Author Share Posted December 31, 2009 Yes, I tried but keep getting error: SQL query: Documentation SELECT * FROM voting AS vt CROSS JOIN vendors AS v USING ( item_id, vendor_id ) WHERE v.vendor_state =46 LIMIT 0 , 30 MySQL said: Documentation #1054 - Unknown column 'item_id' in 'from clause' item_id is on the table "voting" and it would be the matched "vendor_id" from vendors table where the state = 46 Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986596 Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 You can't use USING in such a case SELECT * FROM voting AS vt INNER JOIN vendors AS v ON vt.item_id = v.vendor_id WHERE v.vendor_state =46 Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986597 Share on other sites More sharing options...
thedepotnetwork Posted December 31, 2009 Author Share Posted December 31, 2009 I am new to this forum stuff, but wow, how does one repay you as a master programmer and help!? Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986598 Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 By becoming my henchman. This includes polishing my shoes and fetching me cold drinks. You can also consider contributing into this thread: http://www.phpfreaks.com/forums/index.php/topic,45685.0.html Quote Link to comment https://forums.phpfreaks.com/topic/186822-tricky-join-help/#findComment-986601 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.