我有一个类似下面的表格,
ID | Amt
-------
1 | $30
2 | $2
3 | $50我想根据ID在数量上做一个选择,问题是ID=1不能保证在那里。因此,我想检查ID=1是否存在,然后选择与ID=1对应的数量,否则我想选择ID=2所在的数量。
所以像这样,
Select (If ID= 1 then amt else amt(for ID2) from Table 如何实现这个Sql select语句?
谢谢。。
发布于 2011-01-21 05:06:44
如果1和2不在那里怎么办?如果它总是跳到下一个id,你应该选择其中的第一个按id排序的项目。
select top 1 amt from table order by id; 这方面的语法可能有点偏差...我目前正在dbs之间跳转...
发布于 2011-01-21 05:08:11
基于你的问题,也许有更好的方法...在不知道更多的情况下,这是我的答案:
MSSQL:
select top 1 * from myTable where ID >= 1 order by IDMySQL:
select * from myTable where ID >= 1 order by ID limit 1发布于 2011-01-21 05:07:37
select Amt from theTable where ID in (select min(ID) from theTable where (ID < 3) )
https://stackoverflow.com/questions/4752543
复制相似问题