Jump to content

Select fields from multiple tables with SQL join


jackh

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

Link to comment
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.

Link to comment
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:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...