Mundo Posted March 30, 2009 Share Posted March 30, 2009 I have the following 2 tables: http://i44.tinypic.com/9gc0oh.jpg http://i39.tinypic.com/9zoug6.jpg Can somebody explain to me how to use a JOIN to link the two "customer_id" together? $result = mysql_query("SELECT * FROM diary WHERE diary_id LIKE '%$_GET[searchterm]%'"); That's my current query... I don't really understand how I would link to it. Quote Link to comment https://forums.phpfreaks.com/topic/151793-mysql-joins/ Share on other sites More sharing options...
Mundo Posted March 30, 2009 Author Share Posted March 30, 2009 $result = mysql_query("SELECT * FROM diary LEFT JOIN customer ON customer_id = customer_id WHERE diary_id LIKE '$_GET[searchterm]'"); My understanding is it works like that but it doesn't seem to work... I want to query: Everything from both the customer and diary tables, where the diary_id but linking the the two tables together by the customer_id Quote Link to comment https://forums.phpfreaks.com/topic/151793-mysql-joins/#findComment-797032 Share on other sites More sharing options...
Mundo Posted March 30, 2009 Author Share Posted March 30, 2009 Okay, I think I sussed it: $result = mysql_query("SELECT * FROM diary, customer WHERE diary_id = '$_GET[searchterm]' AND diary.customer_id = customer.customer_id"); Thanks for the help, guys. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/151793-mysql-joins/#findComment-797071 Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 You weren't telling MySQL what tables the fields came from. You could have done something like this: $result = mysql_query("SELECT * FROM diary d LEFT JOIN customer c ON c.customer_id = d.customer_id WHERE d.diary_id LIKE '$_GET[searchterm]'"); FYI: You should really sanitize your values before you put them in a query, even though it's an integer, it's still a good practice to use mysq_real_escape_string(). Quote Link to comment https://forums.phpfreaks.com/topic/151793-mysql-joins/#findComment-797087 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.