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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:34:11+00:00 2026-06-17T14:34:11+00:00

I have a quite unique need to make select always return one row My

  • 0

I have a quite unique need to make select always return one row

My SQL:

select * from table1 Where (table1.pk = :p1) or (table1.fk1 = :p1)

The above SQL always has two cases for return:

1- my Select return two records:
The only different is one of the records has data while the other has only the ID filled with data while the rest of its fields are null. I need in this case to return only the one that has data in other fields.

2- my Select return one record
In this case the record returned has only the ID field filled with data while the rest of the fields are null however this is what I want and no need for any further processing.

Please advise if is it possible to do that in one plain Select SQL. I can not use stored procedure.

  • 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-17T14:34:12+00:00Added an answer on June 17, 2026 at 2:34 pm

    You can use the first clause of the select statement to get only 1 row.

    Given your specific conditions, you can order the result set descending by the rest of the fields to be sure the null row is selected only in case there’s no data row (null goes first in firebird 2.5, but AFAIK this changed somewhere in the last versions, so check your specific version before applying this).

    Your final query will look like this:

    select first 1 * 
      from table1 
     where (table1.pk = :p1) 
        or (table1.fk1 = :p1)
     order by somecolumn;
    

    somecolumn being the most relevant of the other fields that can contain null values.

    you can test this with this statements:

    --two rows, one with ID and values and the other with ID and null
    with q1 as (
          select 1 id, 'data' othercolumn
            from rdb$database
          union
          select 2 id, null othercolumn
            from rdb$database
         ) 
    select first 1 *
      from q1
     order by othercolumn nulls last;
    
    --versus:
    
    --onw row with ID and null
    with q1 as (
          select 2 id, null othercolumn
            from rdb$database
         ) 
    select first 1 *
      from q1
     order by othercolumn nulls last;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite a complex piece of code (JQuery and HTML5 Web SQL) that
I have quite big document in html format that generated from Microsoft Word. It
I have quite a lot of C++ legacy code modules from my colleagues, unfortunately
I have a quite big custom made config file I need to extract data
I need to select all of the rows from the database where there is
I need to have a custom unique identifier (sequence). In my table there is
I always seem to have a problem when I need to compare 2 list
we have quite a lot of RPG-programs here, and we do a lot of
I have quite a few buttons in my application, they vary in sizes based
I have quite a problem concerning the use of relational database concepts in Delphi

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.