简体   繁体   English

将空运算符存储在SQL数据库中时如何在SQL查询中执行空运算

[英]How to perform airthmetic operation in SQL query when airthmatic operators are stored in SQL database

I have a table like this: 我有一张这样的桌子:

id  a     b    operation    c
-------------------------------
1    2    3       +          5
2    2    3       +          6
3    3    2       /          1
4    4    7       *         28
5   54    2       -         27
6    3    0       /          0

When we have to perform a simple arithmetic operation we simply use 当我们必须执行简单的算术运算时,我们只需使用

 select a+b from table;

I have to use the operation column to check the value of a operation b is equal to c or not 我必须使用操作列来检查操作b的值是否等于c

Using "case" would help in your ... case 使用“ case”将有助于您的... case

select a,operation,b,'=',
       CASE operation
           WHEN '+' then a+b 
           WHEN '*' then a*b 
           WHEN '-' then a-b
           WHEN '/' then if(b=0,'NAN',a/b)
           else null
       end AS c
from mytable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM