SQL:称为IN的列导致查询错误

时间:2019-05-02 10:49:59

标签: sql

我正在尝试运行一个SQL查询,该查询包含一个其他人称为“ IN”的关键列:

SELECT id, count(v1) as v1, AVG(IN) as code, SUM(v2) as v2
FROM datatable
GROUP BY id, IN

正在导致错误:

  

关键字“ IN”附近的语法不正确。

可能将其误认为是IN SQL运算符。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

如果使用SQL Server,则可以使用[]

 SELECT id, count(v1) as v1, AVG([IN]) as code, SUM(v2) as v2
    FROM datatable
    GROUP BY id,[IN]

或者如果您有mysql,请使用反斜线``

SELECT id, count(v1) as v1, AVG(`IN`) as code, SUM(v2) as v2
        FROM datatable
        GROUP BY id,`IN`

答案 1 :(得分:1)

我认为您可以通过将表名放在列的开头来解决此问题:

SELECT id, count(v1) as v1, AVG(datatable.IN) as code, SUM(v2) as v2
FROM datatable
GROUP BY id, IN

或者您也可以将IN放在[]