日期:2014-05-18  浏览次数:20531 次

select max的问题,如果表中无记录能否让其返回1?
表结构:
song(id   int,   name   vchar(50))
request(id   int,   songid,   playseq,   played)

SELECT           song.id,   MAX(requested.playseq)   +   1   AS   playseq
FROM                   song   CROSS   JOIN
                                            request   AS   requested
WHERE           (song.name   =   'pclili ')
GROUP   BY   song.id

上面的语句在request里有记录时,可以成功执行,但若里面没有任何数据时,却查不到任何数据,能否让request里没有任何记录时也成功插入?

谢谢指点.在线等待!

------解决方案--------------------
--try
isnull(MAX(requested.playseq),1)
------解决方案--------------------
SELECT song.id, isnull(max(requested.playseq),0) + 1 AS playseq
FROM song CROSS JOIN
request AS requested
WHERE (song.name = 'pclili ')
GROUP BY song.id
------解决方案--------------------
song(id int, name vchar(50))
request(id int, songid, playseq, played)

SELECT song.id, isnull(MAX(requested.playseq),1) + 1 AS playseq
FROM song CROSS JOIN
request AS requested
WHERE (song.name = 'pclili ')
GROUP BY song.id