SELECT MIN(retail) FROM books WHERE category = ‘COMPUTER’
效果很好,但是当我在select中包含标题时,例如:
SELECT MIN(retail), title FROM books WHERE category = 'COMPUTER'
没有。为什么?如何使其运作?
如果那是您的意思,Rhys的答案是正确的,但是您可能想在title()中使用retail=MIN(retail),而该措辞建议了如何获得该答案:
title
retail=MIN(retail)
SELECT title, retail FROM books WHERE category = 'COMPUTER' AND retail = (SELECT MIN(retail) FROM books WHERE category = 'COMPUTER')
为了减少重复,可以使用WITH子句(如果使用的是最新版本的SQL):
WITH
;WITH ComputerBooks AS ( SELECT title, retail FROM books WHERE category = 'COMPUTER') SELECT title, retail FROM ComputerBooks WHERE retail = (SELECT MIN(retail) FROM ComputerBooks)