Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic Operation in MySQL

I want to execute a SQL query in mysql something like the below:

SELECT
(0-5) AS `sqlTest`, if(`sqlTest` < 0, 'false', 'true')

But I am getting an error "Unknown column 'sqlTest' in 'field list'"

Note

This is not an exact query. Here the operands 0 and 5 will be replaced with 2 big Select queries.

like image 769
Ramesh India Avatar asked Mar 19 '26 15:03

Ramesh India


2 Answers

Hey i got the answer of your question. here,

try this,

SELECT @var:=(0-5) AS sqlTest, if(@var< 0, 'false', 'true');

like image 98
ketan Avatar answered Mar 21 '26 05:03

ketan


Since you're using MySQL, you can use a temporary to solve it;

SELECT @tmp := (0-5) AS `sqlTest`, if(@tmp < 0, 'false', 'true')

Simple demo here.

like image 35
Joachim Isaksson Avatar answered Mar 21 '26 06:03

Joachim Isaksson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!