We are using absolute and relative discount methods in our sales department. Most of the times the customer asks for discount in Absolut values like 3 Baht discount on a 28 Baht Product Price, and our foreign customers will typically ask for 10 % discount in the same situation.
We do this on our old MS Access platform by selecting an absolute or relative discount indicator and by having a Calculated Fields for the new selling price in our tables:
ord_det_id AutoNumber
prod_s_price Number "The sales price Looked Up from the product table"
disc_ind_i Number "Discount Indicater. 1 = Absolute 2 = Relative"
disc_prod_s_price Calculated
The expression for the calculated field is: IIf([disc_ind_id]=1,[prod_s_price]-[disc],[prod_s_price]-([prod_s_price]*[disc]/100))
When I run following Query in my MySQL Workbench I receive the expected results for the Discounted Product Price:
SELECT IF(disc_ind_id = 1, prod_s_price-disc, prod_s_price-(prod_s_price*disc/100)) AS disc_prod_s_price FROM tbl_0_ord_det;
I have tried to make a new table with a generated column in Mysql, where I expected the same result as in my MS Access application:
CREATE TABLE `tbl_0_ord_det` (
`ord_det_id` int NOT NULL,
`prod_s_price` double DEFAULT NULL,
`disc_ind_id` smallint DEFAULT NULL,
`disc` double DEFAULT NULL,
`prod_s_price_disc` double GENERATED ALWAYS AS (SELECT IF(disc_ind_id = 1, prod_s_price-disc, prod_s_price-(prod_s_price*disc/100))));
I am getting following error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT IF(disc_ind_id = 1, prod_s_price-disc, prod_s_price-(prod_s_price*disc/10' at line 6
What am I doing wrong?