Jump to content

MySQL Joins


Mundo

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.