Jump to content

Select the difference between 2 column


ayok

Recommended Posts

Hi,

I'm trying to select the difference between column 'original_price' and 'price' which is less than certain amount. Here is my query.

SELECT *,(original_price - price) AS profit FROM items WHERE profit <= '50.00' ORDER BY RAND()

 

It gives me a mysql error that profit is not exist. Would someone tell me how to get expected result?

Thank you.

 

ayok

Link to comment
Share on other sites

You can't use aliases in WHERE clauses - pretend that aliasing happens after the conditions.

 

HAVING doesn't have that restriction.

SELECT *, (original_price - price) AS profit FROM items HAVING profit 

 

While I'm at it,

1. If a number is supposed to be a number, don't put quotes around it.

2. Are you sure you want to ORDER BY RAND()? Do you actually want [i]all[/i] of the results in a random order? Or do you just want a couple of them chosen randomly?

Link to comment
Share on other sites

You can't use aliases in WHERE clauses - pretend that aliasing happens after the conditions.

 

HAVING doesn't have that restriction.

SELECT *, (original_price - price) AS profit FROM items HAVING profit <= 50.00 ORDER BY RAND()

 

While I'm at it,

1. If a number is supposed to be a number, don't put quotes around it.

2. Are you sure you want to ORDER BY RAND()? Do you actually want all of the results in a random order? Or do you just want a couple of them chosen randomly?

 

Thanks..

I've tried using HAVING, and still got error. Can't I have HAVING and WHERE in a query?

Yes I plan to display those randomly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.