Jessica Posted April 11, 2008 Share Posted April 11, 2008 Hey guys. I am trying to get info out of a database. There is a table of "foods" and a table of "brands". Some foods have a brandID, and some don't (0). I want to select all of the foods and their brand. The problem is when I do this: SELECT food.*, brands.* WHERE food.brandID = brand.brandID I don't get the food items that don't have a brand. The obvious solutions is to do 2 sql statements, selecting the foods and then the brands, then using PHP to put them together. But, is there a way to do it in one statement? Thanks! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 11, 2008 Share Posted April 11, 2008 Try: SELECT * FROM food LEFT JOIN brands ON food.brandID = brands.brandID Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 11, 2008 Author Share Posted April 11, 2008 Awesome, the only thing I had to do was add the brands.* to get the actual brand info. SELECT food.*, brands.* FROM food LEFT JOIN brands ON food.brandID = brands.brandID Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.