日期:2014-05-17 浏览次数:20582 次
SELECT TOP 1 * FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18' ) AS A ORDER BY 数量 DESC SELECT TOP 1 * FROM TEST ORDER BY 日期 DESC
SELECT TOP 1 * FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16' AND '2012-7-18' ) AS A ORDER BY 数量 DESC IF @@ROWCOUNT = 0 BEGIN SELECT TOP 1 * FROM TEST ORDER BY 日期 DESC END
------解决方案--------------------
如果第一条语句有结果,则执行
就是两个select 都要呢 还是执行第一个 第二个不执行?
如果是继续执行那么参考2、3楼
如果是第二个不执行
if exists(SELECT 1 FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18') SELECT TOP 1 * FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18' ) AS A ORDER BY 数量 DESC else SELECT TOP 1 * FROM TEST ORDER BY 日期 DESC
------解决方案--------------------
if exists(SELECT TOP 1 * FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18' ) AS A ORDER BY 数量 DESC) SELECT TOP 1 * FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18' ) AS A ORDER BY 数量 DESC else SELECT TOP 1 * FROM TEST ORDER BY 日期 DESC