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 1108045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:01:23+00:00 2026-05-17T02:01:23+00:00

Using MySQL, I’ve got a set of tables with 1->N relationships from member ->

  • 0

Using MySQL, I’ve got a set of tables with 1->N relationships from member -> items -> photos.

I’m trying to select a list of a member’s items + some columns from the first associated photo (by first I mean the photo with the lowest sort order).

Currently I have this query which works, but seems overly complex. Is there an easier way and/or how could this query be improved. In particular, I’m concerned about the inner most select statement – will MySQL optimize this down to only work with photos for the member in question and should I be using additional where clauses in there to help optimize it.

SELECT items.member_id, items.item_id, items.title, p.photo_id, p.blob_id, p.image_width, p.image_height
FROM items
LEFT JOIN
(
    SELECT photos.item_id, photos.photo_id, photos.blob_id, photos.image_width, photos.image_height
    FROM 
    (
        SELECT item_id, min(sort_order) as min_sort_order
        FROM photos
        GROUP BY item_id
    ) AS x 
    INNER JOIN photos on photos.item_id=x.item_id and photos.sort_order=x.min_sort_order
) AS p ON items.item_id = p.item_id
WHERE items.member_id=1
ORDER BY items.title;
  • 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-17T02:01:24+00:00Added an answer on May 17, 2026 at 2:01 am
    SELECT 
            items.member_id, items.item_id, items.title, photo.photo_id, photo.blob_id, photo.image_width, photo.image_height
    FROM 
            items
    JOIN photos on items.item_id = photos.item_id
    JOIN (SELECT item_id, min(sort_order) as min_sort_order
            FROM photos
            GROUP BY item_id) min_photo on photos.item_id=min_photo.item_id and photos.sort_order=min_photo.min_sort_order
    WHERE items.member_id=1
    ORDER BY items.title;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MySQL Administrator GUI tool I have exported some data tables retrieved from an
Using MySQL and PHP. I'm trying to select 4 random ads from a table.
Using MySQL, I have a number of queries that look like: SELECT * FROM
Using MySQL 5.x I want to efficiently select all rows from table X where
Using mysql this would be: SELECT * FROM thetable ORDER BY id DESC LIMIT
using mysql as database. I got this code from the previous answers to the
Using Mysql How would i merge these two commands into one? select id from
Using MySQL, I want to return a list of parents, from a table that
Using MySQL , I can do something like: SELECT hobbies FROM peoples_hobbies WHERE person_id
Using MySQL, How do I import a user defined database function from one db

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.