I have two tables in a database. One is for products and the other is for product images. I’m joining the tables so that I can get the product images and info with one query:
SELECT *
from items left join images
on items.item_id = images.item_id and
items.display_items = '1' and items.active = '1'
order by items.item_year desc, items.fullname desc, images.position asc
I noticed, however, that all of the products weren’t showing up. Some of the products don’t have any images and those were the ones not appearing. I ran my query in Sequel Pro and saw that the column item_id was appearing twice, once with the id and once as NULL. How can I fix my query to get all of the products?
I would revise your query to specify the exact columns you want in your result rather than use “select *”. In this case, you may have columns with the same name in the 2 tables (such as “item_id”), and this can cause problems in “select *”.
Also, I would take the 2nd line of your ON statement and put it in a WHERE clause. The “items.display_items = ‘1’ and items.active = ‘1’” doesn’t apply to your join, but rather to the set of items you want.
Revised query: