Imran Khan
Imran Khan

Reputation: 2401

Selecting rows from a table by One a field from other table

What i want, to display rows from a table which is selected by a field from other table single value, lets say to display images from a table by last category id.

I have this type of query, but this return me all matching keys rows, if i inset LIMIT 1 then it return one row...

SELECT i.prof_image FROM profile_images i
JOIN images_cat cat ON (cat.cat_id = i.cat_id)
GROUP BY i.prof_image; 
//OR LIMIT 1;

Any idea to fix this problem. (i.e. displaying the latest category images)?

Upvotes: 0

Views: 109

Answers (2)

StevieG
StevieG

Reputation: 8709

This will work for your specific example.. If you need to be more selective, then please post some more details..

SELECT i.prof_image 
FROM profile_images i 
WHERE cat_id = (select max(cat_id) from images_cat)

Upvotes: 1

Mr Meow
Mr Meow

Reputation: 362

SELECT * FROM table_1
LEFT JOIN table_2 ON table_1.id = table_2.id

This query will grab all things in table_2 that have the same id value.

Note that it is a LEFT JOIN - which means that if there are no matching values in table_2, it will still return the values from table_1.

What is your intent with using last()?

Hope this helps.

Upvotes: 1

Related Questions