0

我需要一起使用 min 和 max 编写一个语句。

SELECT companyname  FROM companies JOIN stocklist 
USING (companyid) where price =some(
( select max(price) from stocklist) , ( select min(price) from stocklist) ) ;

我需要获得最低和最高价值的公司名称,但这不起作用(因为最后一行)出了什么问题,我应该怎么做?

更新:

select companyname from stockList join companies using (companyid) WHERE price IN (select min(price) from stocklist) ;

这对我有用,但只有一个操作数,我怎样才能得到两个?

4

2 回答 2

1

I don't like the idea of having two subqueries in an IN clause. Here is another way with the join being made explicit:

SELECT companyname 
FROM companies c JOIN
     stocklist s
     c.companyid = s.companyid join
     (select min(price) as minprice, max(price) as maxprice
      from stocklist
     ) sm
     on s.price = minprice or s.price = maxprice
于 2012-08-19T20:21:40.197 回答
0

尝试这个:

SELECT companyname  FROM companies c
INNER JOIN stocklist s
ON c.companyid = s.companyid
WHERE price IN
((SELECT MAX(price) FROM stocklist ), 
 (SELECT MIN(price) FROM stocklist ))

看这个小提琴

于 2012-08-19T14:24:22.277 回答