Arithmetic operation in MySQL

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

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

But I get the error "Unknown column" sqlTest "in the" list of fields "

Note

This is not an exact request. Here operands 0 and 5 will be replaced with two large queries.

Any help would be appreciated

Thanks in advance.

+4
source share
4 answers

Since you are using MySQL, you can use a workaround to solve it;

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

Simple demo here .

+2
source
Hi, I got the answer to your question. here,

try it,

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

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

I organized a subquery with a dummy id:

 select if(x.`sqlTest` < 0, 'false', 'true') from (SELECT (0-5) AS `sqlTest`) x 
+1
source

Source: https://habr.com/ru/post/1400704/


All Articles