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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:53:59+00:00 2026-06-17T12:53:59+00:00

I have table ‘Images’ I want to get the main image if exist, and

  • 0

I have table ‘Images’ I want to get the main image if exist, and if not, get the first row in the table. I use this:

SELECT Students.Id, Images.ImagePath
FROM Students INNER JOIN    
     Images 
     ON  Students.Id = Images.StudentId 
     AND (   (Images.IsMain = 1) 
          OR (Images.ImagePath = (
              SELECT TOP(1) ImagePath 
              FROM  Images 
              WHERE StudentId = Students.IdId)))
WHERE Students.Id = @StudentId

If there is no ‘IsMain’ in Images I get only one row, but if there is ‘IsMain’, I get the row twice.

http://sqlfiddle.com/#!3/3ade7/1

  • 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-06-17T12:54:00+00:00Added an answer on June 17, 2026 at 12:54 pm

    Your problem is that you were selecting the IsMain image and the first image.

    The following code selects the first row, which will be the IsMain one if it exists, otherwise the top row.

    SELECT Students.Id, Images.ImagePath
    FROM Students INNER JOIN    
        Images 
        ON  Students.Id = Images.StudentId 
        AND Images.ImagePath = (
            SELECT TOP(1) ImagePath 
            FROM Images 
            WHERE StudentId = Students.Id
            ORDER BY IsMain DESC) -- This line forces the IsMain image to be the top image returned.
    WHERE Students.Id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with 300 000 records (MyISAM). I get record from this table
i have table that looks like this: <table class=content> <tbody> <tr class=fc-first> <td class=fc-widget-content
I have table, and I want check/uncheckcheck box not only by clicking on checkbox,
I have table and this table contain result column with some entries. I just
I have table View with grouped style with three sections. Get information from dictionary
I have table with two columns (varchar from, varchar to). This table represents connections
I have table stucture: table { `id` int(11) NOT NULL AUTO_INCREMENT, `content_type`, `protocol`, `content_id`,
I have table: test and field called: id. I want update time (ex. add
I have table in my database CREATE TABLE user_user ( id bigint NOT NULL,
I have table with structure like this: -------------------------------------------- | id | tid | uid

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.