Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 977167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:51:42+00:00 2026-05-16T03:51:42+00:00

I have a table containing persons information (one row per person) and another table

  • 0

I have a table containing persons information (one row per person) and another table containing persons photos filenames (many rows per person). I want to select a group of persons (based on another table) but only one photo per person.

My old SQL was like this:

SELECT persons.personID, persons.name, persons.photo_filename, movie_cast.role
FROM persons, movie_cast
WHERE persons.personID = movie_cast.personID
AND movie_cast.imdbID = ?
ORDER BY movie_cast.castORDER
LIMIT 9';

But here, the ‘persons’ table contains also a ‘photo_filename’ column. In my new database design, this column is in another table. So if I try to get the photo_filename from the new table I get all the photos available for each person, but I need to get only one.

How to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:51:42+00:00Added an answer on May 16, 2026 at 3:51 am

    In the first example I have assumed there is always a photo, and am just grabbing the highest sorted photo filename alphabetically as a means to get a consistent photo for each user each time you run the query.

    SELECT p.personID, p.name, ph.photo_filename, mc.role 
    FROM persons p
    INNER JOIN movie_cast mc ON p.personID = mc.personID 
    INNER JOIN (
        select personID, max(photo_filename) as MaxPhotoName
        from photos
        group by personID
    ) phm on p.personID = phm.personID
    INNER JOIN photos ph on phm.personID = ph.personID 
        and phm.MaxPhotoName = ph.photo_filename
    WHERE mc.imdbID = ? 
    ORDER BY mc.cast 
    LIMIT 9
    

    If there is a photo_date column and you want to use the newest photo you can do it like this:

    SELECT p.personID, p.name, ph.photo_filename, mc.role 
    FROM persons p
    INNER JOIN movie_cast mc ON p.personID = mc.personID 
    INNER JOIN (
        select personID, max(photo_date) as MaxPhotoDate
        from photos
        group by personID
    ) phm on p.personID = phm.personID
    INNER JOIN photos ph on phm.personID = ph.personID 
        and phm.MaxPhotoDate = ph.photo_date
    WHERE mc.imdbID = ? 
    ORDER BY mc.cast 
    LIMIT 9
    

    If there is not always a photo, you can use a LEFT OUTER JOIN so that you will still get all your records back:

    SELECT p.personID, p.name, ph.photo_filename, mc.role 
    FROM persons p
    INNER JOIN movie_cast mc ON p.personID = mc.personID 
    LEFT OUTER JOIN (
        select personID, max(photo_date) as MaxPhotoDate
        from photos
        group by personID
    ) phm on p.personID = phm.personID
    LEFT OUTER JOIN photos ph on phm.personID = ph.personID 
        and phm.MaxPhotoDate = ph.photo_date
    WHERE mc.imdbID = ? 
    ORDER BY mc.cast 
    LIMIT 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing a large number of rows. Each row has 2
I have a table containing a large number of rows. Each row has 2
Here's my problem. Suppose I have a table called persons containing, among other things,
I have table containing data . In every row there is a checkbox plus
I have a table containing data and one of the fields is 'class' When
I have a table containing roles. Many users can be assigned to a role.
I have a mysql table containing 400,000 rows whenever I run a PHP script
I have a table containing a foreign key and I know the column name
I'm making a simplistic trivial pursuit game . I'm not sure if (and then
(related to Finding the lowest unused unique id in a list and Getting unused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.