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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:27:41+00:00 2026-05-26T17:27:41+00:00

I’m trying to figure out how to run a query that would go something

  • 0

I’m trying to figure out how to run a query that would go something like this:

$query = "SELECT RegPrice, OnSalePrice

Math.abs(Math.max(100 - (OnSalePrice) / (RegPrice) * 100)) AS DiscountIS

FROM Products";

So it would take RegPrice & OnSalePrice and give me the percentage of the discount and return it as DiscountIS along with the other 2 values.

Query TWO & Get back THREE.

Any ideas?? Thanks!

  • 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-26T17:27:41+00:00Added an answer on May 26, 2026 at 5:27 pm

    Your query as written is syntactically correct except that you’re missing a comma after OnSalePrice and you shouldn’t put Math. in front of abs or max. You should also probably end it with a semicolon, but that doesn’t usually matter. So, the correct query would be:

    $query = "SELECT RegPrice, OnSalePrice, ABS(MAX(100 - (OnSalePrice) / (RegPrice) * 100)) AS DiscountIS FROM Products;";
    

    (I’ve also capitalized ABS and MAX, but that’s just a style choice. SQL ignores case in all commands and functions.)

    However, there’s problem with this query, which is that MAX is an aggregate function, so it returns the maximum value for the whole table. As such, when using aggregate functions, like MAX, you can’t also select other attributes. So if you want each item to show its RegPrice, OnSalePrice, and the discount, the correct way to do this is to simply leave out MAX. (I suspect that this is what you want to do, since the Math.max function only gives the maximum of its arguments. But I’m not sure because you’ve only given it one argument in this case.)

    $query = "SELECT RegPrice, OnSalePrice, ABS(100 - (OnSalePrice) / (RegPrice) * 100) AS DiscountIS FROM Products;";
    

    If you actually want to know the regular price, sale price, and discount of whichever one has the biggest discount, that’s a slightly more complex query which has to be handled by doing a SELECT within a SELECT.

    $query = "SELECT RegPrice, OnSalePrice, ABS(100 - (OnSalePrice) / (RegPrice) * 100) AS DiscountIS FROM Products WHERE ABS(100-OnSalePrice/RegPrice*100)=(SELECT ABS(MAX(100-OnSalePrice/RegPrice*100)));";
    

    It should be noted, though, that as written, in any version, if OnSalePrice is bigger than RegPrice, then this will still show as a discount and that if the RegPrice is zero, a division by zero will result.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6

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.