I have two tables table_entries
and table_images
.
table_images
holds a series of images for any single item in table_entries
both tables are organized by a vid
which is essentially an item id.
Not all items in table_entries
will have an image, while others may have multiple images
What I am trying to construct is a query to only select items from table_entries
that have an image, specifically 10 entries that have an image, which i've built as follows:
SELECT * FROM table_entries
INNER JOIN table_images ON (table_entries.vid = table_images.vid)
WHERE (model LIKE '%apple%' OR ext LIKE '%apple%')
ORDER BY lastupdated DESC LIMIT 0,10
EDIT: lastupdated
, model
and ext
all belong to table_entries
, in fact all sorting, selecting, etc.. is done based on table_entries
The problem with the above query is that it is successfully only picking items with images, but if a single item has 10 images, then it will return the 10 rows as item #1 with each of its images as individual rows. The intention is to have 10 distinct items, and limit the join to only 1 image for each item from table_entries
So I want to somehow limit the join to 1 row for the entire join.
I've searched for the answer here on SO and found so many good answers to this question (MySQL INNER JOIN select only one row from second table, MySQL JOIN with LIMIT 1 on joined table) , however they all use aliases for selecting the tables.
I could use alias and fix my query, but there is a TON of PHP code that I would have to change to deal with the aliased table names.
Is it possible to fix my query to only select 1 row from the joined table without the use of aliases?