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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:16:09+00:00 2026-05-15T11:16:09+00:00

I have the following mysql table called pics, with the following fields and sample

  • 0

I have the following mysql table called “pics”, with the following fields and sample data:

id   vehicle_id    filename    priority
1    45            a.jpg       4
2    45            b.jpg       1
3    56            f.jpg       4
4    67            cc.jpg      4
5    45            kt.jpg      3
6    67            gg.jpg      1

Is it possible, in a single query, to get one row for each vehicle_id, and the row be the highest priority?

The result I’m looking for:

array (
  [0] => array( [id] => '2', [vehicle_id] => '45', [filename] => 'b.jpg',  [priority] => '1' ),
  [1] => array( [id] => '3', [vehicle_id] => '56', [filename] => 'f.jpg',  [priority] => '4' ),
  [2] => array( [id] => '6', [vehicle_id] => '67', [filename] => 'gg.jpg', [priority] => '1' )
);

If not possible in a single query, what would be the best approach?

Thanks!

  • 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-15T11:16:10+00:00Added an answer on May 15, 2026 at 11:16 am

    While this may be the ‘accepted’ answer, the performance of Mark’s solution is under normal circumstances many times better, and equally valid for the question, so by all means, go for his solution in production!


    SELECT a.id, a.vehicle_id, a.filename, a.priority
    FROM pics a
    LEFT JOIN pics b               -- JOIN for priority
    ON b.vehicle_id = a.vehicle_id 
    AND b.priority > a.priority
    LEFT JOIN pics c               -- JOIN for priority ties
    ON c.vehicle_id = a.vehicle_id 
    AND c.priority = a.priority 
    AND c.id < a.id
    WHERE b.id IS NULL AND c.id IS NULL
    

    Assuming ‘id’ is a non-nullable column.

    [edit]: my bad, need second join, cannot do it with just one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following mysql table called test. lets say it has the data
I have a php variable: $foo My MySQL table called data has the following
I have a simple mysql table called messages with the following fields: ID(int) MESSAGE(varchar)
I have a mysql table called users with the following fields: username - password
I have a MySQL table called blocks with the following fields: blockid blockerloginid blockeeprofile
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have a table called link_track with the following fields: id, uid, ip, hitdate
I have a MySQL table called Hotel . in that i have the following
I have a MySQL table comment with the following fields: loginid submissionid points I
I have a mysql table called jos_users_quizzes with the following columns: id quiz_i duser_id

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.