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

The Archive Base Latest Questions

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

I have a table with the following: +—-+——-+——-+———–+———–+————–+————–+ | id | stock | price

  • 0

I have a table with the following:

+----+-------+-------+-----------+-----------+--------------+--------------+
| id | stock | price | usedStock | usedPrice | specialStock | specialPrice |
+----+-------+-------+-----------+-----------+--------------+--------------+
| #1 |     1 | 10.00 |         0 | 0         |            0 | 0            |
| #2 |     0 | 0     |         1 | 15.00     |            1 | 20.00        |
| #3 |     0 | 0     |         0 | 11.00     |            1 | 14.00        |
+----+-------+-------+-----------+-----------+--------------+--------------+

I would like to create a query that orders by lowest price first if the type of item is in stock.

So the query would result in the following order:

#1 - 10.00 (because 10.00 is the lowest result)
#3 - 14.00 (because although 11.00 is less, it's not in stock)
#2 - 15.00 (because 15.00 is lower than 20.00)

I’ve added the PHP tag to this question in case there’s a quicker way of calculating using PHP after selecting the table.

  • 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-18T06:18:00+00:00Added an answer on June 18, 2026 at 6:18 am

    I would order by the least price, in case item is present in stock:

    ORDER BY
      LEAST(CASE WHEN stock        THEN price        ELSE GREATEST(price, usedPrice, specialPrice) END,
            CASE WHEN usedStock    THEN usedPrice    ELSE GREATEST(price, usedPrice, specialPrice) END,
            CASE WHEN specialStock THEN specialPrice ELSE GREATEST(price, usedPrice, specialPrice) END)
    

    If the condition is true (>=1), each CASE WHEN will return the price, otherwise it will return the greatest price. Fiddle here.

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

Sidebar

Related Questions

I have DB2 table having following structure CREATE TABLE DUMMY ( ID CHARACTER(10) NOT
I have following table in my database. I am trying to write a query
I have a table like following,' <table id=online_seat> <tbody id=row_B class=selected> <tr class=seat_01 B
I have following table CREATE TABLE User ( email sysname NOT NULL, sign varbinary(256)
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have a table like the following table: UserID Num1 Num2 Code Name Cat
I have following table schema - CREATE TABLE [dbo].[TEST_TABLE] ( [TEST_TABLE_ID] [int] IDENTITY(1,1) NOT
I have following table in my database. ID DATE HOTEL_NAME PRICE 1 16-01-2012 AA
I have following table: FOLDER[ id int, name varchar2(10), parent_folder_id int ] I would
I am using MySQL 5.0. I have table with following structure and data. CREATE

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.