jackh Posted December 15, 2016 Share Posted December 15, 2016 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 More sharing options...
justsomeguy Posted December 15, 2016 Share Posted December 15, 2016 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 More sharing options...
jackh Posted December 18, 2016 Author Share Posted December 18, 2016 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now