Jump to content
Sign in to follow this  
jackh

Select fields from multiple tables with SQL join

Recommended Posts

Hi all,

 

I'm trying to create a query which selects fields from 2 tables.

I have a table 'stock' to store cars for sale and a table called 'custstock' which has the customer username and the vehID.

How do I show all the cars that relate the that user who is logged in?

 

My current query that doesn't work;

$username = "-1";
if(isset($_SESSION['username'])){
	$username = $_SESSION['username'];
}
// executeable query
$sql = ("SELECT *
		FROM [stock] JOIN custstock
		ON [stock].custstockvehID = custstock.vehID
		WHERE username = '$username'");

Thanks,

Jack

Share this post


Link to post
Share on other sites

You don't need a join for that, you can just select all of the records from the custstock table for the particular user. Otherwise, make sure your database supports the square bracket syntax, and it would be better to move the where condition into the join condition so that it doesn't spend the time to join all possible records before filtering out the ones it will return.

Share this post


Link to post
Share on other sites

You don't need a join for that, you can just select all of the records from the custstock table for the particular user. Otherwise, make sure your database supports the square bracket syntax, and it would be better to move the where condition into the join condition so that it doesn't spend the time to join all possible records before filtering out the ones it will return.

Cheers, justsomeguy - got it working :good:

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...