Jump to content

Recommended Posts

I have the following 2 tables:

 

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.

 

Link to comment
https://forums.phpfreaks.com/topic/151793-mysql-joins/
Share on other sites

		$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

Link to comment
https://forums.phpfreaks.com/topic/151793-mysql-joins/#findComment-797032
Share on other sites

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().

Link to comment
https://forums.phpfreaks.com/topic/151793-mysql-joins/#findComment-797087
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.