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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:21:01+00:00 2026-06-03T03:21:01+00:00

I have a table that has 5 columns: uniqueid , int1 , int2 ,

  • 0

I have a table that has 5 columns: uniqueid, int1, int2, int3, and FruitStand. What I’m looking for is an example on how to do a single select that outputs the data of whichever column has the least non-null, non-zero integer.

Example Table:

uniqueid|int1|int2|int3|FruitStand
1       |   2|   3|   4|Apples
2       |  21|   4|   0|Oranges
3       |NULL|   2|   5|Pears

So I would get the results from this query of

1       |   2|Apples
2       |   4|Oranges
3       |   2|Pears
  • 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-03T03:21:02+00:00Added an answer on June 3, 2026 at 3:21 am
    SELECT uniqueid,
    FruitStand,
    LEAST(
        COALESCE(int1, int2, int3),
        COALESCE(int2, int3, int1),
        COALESCE(int3, int1, int2)) AS number
    FROM myTable
    

    For readability, I omitted the NULLIF(intX, 0) for each argument in COALESCE. You will need to add those to ignore zero values.

    You will need to use COALESCE to avoid NULL values when using the LEAST function because as of MySQL 5.0.13, LEAST returns NULL if any argument is NULL.

    See this question for details on that.

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

Sidebar

Related Questions

I have a SQlite3 table that has typeless columns like in this example: CREATE
I have a table that has two columns: CommunityID PersonID And A People table
I have a table that has 5 columns: AcctId (int), Address1 (varchar), Address2 (varchar),
Screenshot mockup: http://tinypic.com/r/y2qex/5 Problem: I have a table that has 53 columns; one for
I have a mysql table that has 2 columns - Column 1 contains a
I have a city table that has two columns from_city to_city now lets assume
I have a table that has three different date columns, so I set each
I have a MSSQL table stores that has the following columns in a table:
I have a table in my data base that has three columns: Screen, Icon,
We have a 10 thousand row table that has just 2 columns, a primary

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.