Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use temp field in arithmetic operation in MySQL

I want to do some arithmetic in query using temporary field.

SELECT point, '5' AS bonus, (bonus*2) AS total FROM arithmetic

but the temp field is unknown

Unknown column 'bonus' in 'field list'

how can i use or called temp field in query?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can either use a user variable here to store the valus of bonus, or use a subquery/CTE. Assuming the latter, and that you are running MySQL 8+, we can try:

WITH cte AS (
    SELECT point, 5 AS bonus
    FROM arithmetic
)

SELECT point, bonus, 2*bonus AS total
FROM cte;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading