SELECT MIN(retail) FROM books WHERE category = 'COMPUTER'
工作正常,但当我在选择中包含标题时:
SELECT MIN(retail),title FROM books WHERE category = 'COMPUTER'
它没有.为什么?如何使它工作?
解决方法
Rhys的答案是正确的,如果这是你的意思,但你可能想要零售= MIN(零售)的标题,并且该措辞建议如何得到答案:
SELECT title,retail FROM books WHERE category = 'COMPUTER' AND retail = (SELECT MIN(retail) FROM books WHERE category = 'COMPUTER')
要减少重复,可以使用WITH子句(如果您使用的是最新版本的sql):
;WITH ComputerBooks AS ( SELECT title,retail FROM books WHERE category = 'COMPUTER') SELECT title,retail FROM ComputerBooks WHERE retail = (SELECT MIN(retail) FROM ComputerBooks)