我知道u_photo_id=4,必须u_document_id=1;怎么查他上一个id和他下一个id的信息 
急用 
u_photo_id  name  sex  u_document_id 
1                a     d         1 
3                f     d         2
4                b     d         1 
5                e     d         2   
7                r     d         1 
6                c     d         1 
 
怎么谢谢 
我要用mysql写,上面问了两次,都是用得top,但是mysql不支持top,谁能告诉我mysql怎样写,谢谢急用
我用
select * from #tb where u_photo_id =(select max(u_photo_id) from #tb where u_photo_id<4)
但是不行,只能查u_photo_id是连续的,如果是不连续的就不行了,就像我给的例子,我知道u_photo_id=4但是查u_photo_id=1就查不了

解决方案 »

  1.   

    --上一个id
    select * from tb where u_photo_ID=(select max(u_photo_id) from tb where u_photo_id <4 and u_document_id=1)
    --下一个id
    select * from tb where u_photo_ID=(select min(u_photo_id) from tb where u_photo_id >4 and u_document_id=1)
      

  2.   

    怎么查他上一个id和他下一个id的信息 
    你上一个和下一个的结果是什么?
      

  3.   

    楼主去MySql版块提可能会更好点,这里的思路都是MSSQL的
      

  4.   


    --你的id不是顺序的,要用临时表来排。
    select px=identity(int,1,1),* into # from #tb
    --上一个id 
    select * from # where u_photo_ID=(select max(u_photo_id) from # where u_photo_id <4 and u_document_id=1) 
    --下一个id 
    select * from # where u_photo_ID=(select min(u_photo_id) from # where u_photo_id >4 and u_document_id=1)
      

  5.   

    create table a(u_photo_id int,name varchar(10),sex varchar(2),u_document_id int);
    insert a select 1,'a','d',1 
    union all select 3,'f','d',2 
    union all select 4,'b','d',1 
    union all select 5,'e','d',2  
    union all select 7,'r','d',1 
    union all select 6,'c','d',1;select * from a where u_document_id=1 and u_photo_id<4 limit 1;
    select * from a where u_document_id=1 and u_photo_id>4 limit 1;