1

我有一张由podcasts做过的表格、歌曲艺术家标题混合编号日期等组成的表格。我正在使用具有单独方法的类来单独显示它们

我的问题是,你如何得到desc行?像这样(mix_number is an int)

 select mix_name, date where mix_number = MAX(mix_number) limit 1 //1st query

 select mix_name, date where mix_number = MAX(mix_number - 1)//this would be the second query

 select mix_name, date where mix_number = MAX(mix_number - 2)//3rd query

我不想where clause用数字硬编码,因为我希望它随着我添加更多而更新。

我真的在问是MAX(mix_number)还是MAX(mix_number-1)适当的?我不能让它以这种方式工作

我希望这是可以理解的。我在方法中有其他疑问,但这里的答案也会解决它们。

4

3 回答 3

1
select mix_name, date 
FROM tableName
where mix_number = (SELECT MAX(mix_number) FROM tableName)
LIMIT 1

或者

select mix_name, date 
FROM tableName
where mix_number = (SELECT MAX(mix_number) FROM tableName) - 1
LIMIT 1
于 2013-01-10T00:26:53.350 回答
1

第一个查询:

SELECT mix_name, date
FROM table
ORDER BY mix_number DESC
LIMIT 1, 0

第二个查询:

SELECT mix_name, date
FROM table
ORDER BY mix_number DESC
LIMIT 1, 1

如果您想要来自同一查询的所有行:

SELECT mix_name, date
FROM table
ORDER BY mix_number DESC

如果您想更精确地对具有相同 mix_number 的行进行排序:

SELECT mix_name, date
FROM table
ORDER BY mix_number DESC, date DESC
于 2013-01-10T00:34:49.593 回答
1

你需要一个ORDER BY子句。

select mix_name, date order by mix_number desc

添加LIMIT 0, 1将是您的第一个查询。LIMIT 1, 1将是第二个。等等

于 2013-01-10T00:37:26.657 回答