1

仍在尝试习惯编写查询,但我遇到了问题。

Select count(region)
where (regionTable.A=1) in
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
               V on jxn.id = V.id inner join
               regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
)

内部查询在一列中给出一个 ID 号,它们出现在表中的次数,如果它们在区域 A 中,则给出一个位属性。外部查询有效,但我得到的错误是incorrect syntax near the keyword IN。在内部查询中,我想知道其中有多少在区域 A

4

4 回答 4

1

您必须在查询中在 where 之前指定表名

  Select count(region)
    from table
    where (regionTable.A=1) in

你必须选择其中之一。

where regionTable.A = 1 

或者

where regionTable.A in (..)
于 2012-05-17T20:03:58.557 回答
1

您的查询有几个语法错误。根据您的评论,我认为不需要子查询,而您想要这样:

select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
               V on jxn.id = V.id inner join
               regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id, regionTable.A

可以进一步简化为:

select jxn.id, count(jxn.id) as counts
     , 1 as A                             --- you can even omit this line
from jxn inner join
               V on jxn.id = V.id inner join
               regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id
于 2012-05-17T20:17:27.940 回答
0

You are getting the error because of this line:

where (regionTable.A=1)

You cannot specify a condition in a where in clause, it should only be column name

于 2012-05-17T19:58:54.643 回答
0

像这样的东西可能是你想要的:

SELECT COUNT(*)
FROM 
    (
        select jxn.id, count(jxn.id) as counts, regionTable.A 
        from 
            jxn inner join
            V on jxn.id = V.id inner join
            regionTable on v.regionID = regionTable.regionID 
        group by jxn.id, regionTable.A
    ) sq
WHERE sq.a = 1
于 2012-05-17T20:06:29.690 回答