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

The Archive Base Latest Questions

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

I have two table for gallery system : gallery_cat( gallery_cat_id PK, gallery_cat_name ) gallery(

  • 0

I have two table for gallery system :

gallery_cat(
  gallery_cat_id    PK,
  gallery_cat_name
)

gallery(
  gallery_id        PK,
  gallery_cat_id    FK,
  gallery_name,
  gallery_file_name,
  gallery_date
)

I need to write a SQL query that return one picture from gallery table for each album, the purpose of this that I need to list the albums with one picture for each.

gallery_name | gallery_cat_name| gallery_file_name
-------------+-----------------+------------------
pic1         | Album1          | pic1.jpg 
  • 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-26T11:16:30+00:00Added an answer on May 26, 2026 at 11:16 am

    This should do the trick:

    SELECT g2.gallery_name, gc2.gallery_cat_name, g2.gallery_file_name
    FROM gallery g2
    INNER JOIN gallery_cat gc2 ON (g2.gallery_cat_id = gc2.gallery_cat_id)
    WHERE g2.gallery_id IN (
      SELECT g.gallery_id     
      FROM gallery g
      GROUP BY g.gallery_cat_id)
    

    Explanation:

    At the end is a sub-select

    IN (
      SELECT g.gallery_id     
      FROM gallery g
      GROUP BY g.gallery_cat_id) <<-- select 1 random g.id per gallery_cat.  
    

    Here I select all g.id, but because of the group by clause it will reduce the results to 1 row per grouped by item. I.e. 1 row (chosen more or less at random) per g.gallery_cat_id.

    Next I do a normal select with a join:

    SELECT g2.gallery_name, gc2.gallery_cat_name, g2.gallery_file_name
    FROM gallery g2
    INNER JOIN gallery_cat gc2 ON (g2.gallery_cat_id = gc2.gallery_cat_id)
    WHERE g2.gallery_id IN (
    

    Because I refer to the same table twice in the same query you have to use an alias(*).
    I select all names and all catnames and all filenames.
    However in the where clause I filter these so that only rows from the sub-select are shown.

    I have to do it this way, because the group by mixes rows into one messed up ow, if I select from that directly I will get values from different rows mixed together, not a good thing.
    By first selecting the id’s I want and then matching full rows to those id I prevent this from happening.

    *(in this case with this kind of subselect that’s not really 100% true, but trust me on the point that it’s always a good idea to alias your tables)

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

Sidebar

Related Questions

I have two table and I should write a select query which join this
I have these two tables: Galleries - {id,title,description} Images - {id,gallery_id,file} Each gallery has
As I am working on Employee Management system, I have two table (for example)
I have two table and I need to copy the data across from SRCServiceUsers
I have two table persons and order. I needto join these two tables. $sql=mysql_query(select
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have two table emptable1(empid,status) emptable2(empid,week) i want to select all the empid whose
i have two table: declare @t1 table (id int) declare @t2 table (id int)
I have two table first is TABLE_SUBJECT and second is TABLE_CHAPTER.Now i want to
i have two table. Records and Villas Records table : ID, VID (Villa 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.