为什么用sql减两个冒号

时间:2018-11-20 12:36:29

标签: sql

代码:

SELECT DISTINCT mss.colon1 AS newcolon1, ms.colon2 AS newcolon2, 

                               (SELECT        COUNT(mmm.numbertop) AS Expr1
                               FROM            listtable AS mmm
                               WHERE        (ms.number = mmm.number) ) AS examplecolon1,


                               (SELECT        COUNT(stls.numbertop) AS Expr1
                               FROM           listtable2 AS stls
                               WHERE        ms.number = stls.number ) AS examplecolon2,


                               examplecolon1 -(2 * examplecolon2) from

........

示例:

减法错误:

examplecolon1-(2 * examplecolon2)

为什么解决了这个问题?

1 个答案:

答案 0 :(得分:0)

您需要使用子查询或CTE(如果后端支持)来执行此操作。即:

select newcolon1, newcolon2,
   examplecolon1, examplecolon2,
   examplecolon1 -(2 * examplecolon2) as myNewColumn
from
(SELECT DISTINCT mss.colon1 AS newcolon1, ms.colon2 AS newcolon2, 
                               (SELECT        COUNT(mmm.numbertop) AS Expr1
                               FROM            listtable AS mmm
                               WHERE        (ms.number = mmm.number) ) AS examplecolon1,
                               (SELECT        COUNT(stls.numbertop) AS Expr1
                               FROM           listtable2 AS stls
                               WHERE        ms.number = stls.number ) AS examplecolon2
from myTable1 ms
[left\right\inner] join  myTable2 mss on ... ) sq;