0

相关架构:

courier(courid:int,courname:str)
city(cid:int,cname:str,zid:int) #zid belonging to some table named zone
courierservice(courid:int,cid:int)

因此,显而易见的关系是快递“服务”城市。我一直在努力让所有快递员都服务于“两个”城市,cname 为 A 和 B。即可能的交叉点。

我可以使用以下方法获得解决方法:

 select courname from courier where courid in (select courid from courierservice
 where cid=(select cid from city where cname='A')) and
 courid in (select courid from courierservice where cid=(select cid from city where cname='B'));

但这看起来有点重。

根据文档,它应该使用以下 All 子查询:

select * from courier where courid in (select courid from courierservice 
where cid = all (select cid from city where cname='A' or cname='B'));

但它返回一个空集。

有什么遗漏吗?

4

3 回答 3

0

尝试替换cid = allas cid in,它应该可以正常工作。

于 2012-10-14T07:35:26.303 回答
0

ALL()返回一个集合。但是当你比较使用'='它时,它会与元素进行比较。所以一个元素不能与一个集合进行比较。如果您想知道该元素是否在集合内,那么您必须使用IN子句而不是'='

于 2012-10-14T07:42:28.843 回答
0

It's meaningless to use = ALL: how can a single courierservice.cid simultaneously be equal to more than one city.cid? ALL is only ever used with a comparison operator that can match more than one value, such as >= or <>: see Subqueries with ALL.

However, you would do well to rewrite your query using JOIN (to be both more concise and more performant):

SELECT courname FROM courier NATURAL JOIN (
  SELECT   courid
  FROM     courierservice JOIN city USING (cid)
  WHERE    cname IN ('A', 'B')
  GROUP BY courid
  HAVING   SUM(cname='A') AND SUM(cname='B')
) t
于 2012-10-14T08:08:30.890 回答