select * from tableA where .... 这句是固定死的order by 有逻辑判断
用户点击某个按钮,可以根据不同的字段排序
if order = "id" then
sql = sql & " order by id desc"
end if
现在字段都是 表A中的,很好排序但是 如何根据 其他表来排序呢,比如 tableB 的time 字段前提 select * from tableA where ....  这句不变,能办到吗?

解决方案 »

  1.   

    --其他表(但必须有关联)
    select * from tableB
    inner join 
    (
    --你的sql
    select * from tableA where ....  
    )tableA on tableB.ID = tableA.ID --两表的关系
    order by tableB.name --其他表字段
      

  2.   

    select * from tableA where .... order by (...tableB...) desc
    类似这样 可以吗?
      

  3.   

    如果你一定要这样写,那是tableB 和 tableA 必须一对一,且只能查一个字段,受到一定的限制
      

  4.   


    --如:
    select * from a074 where 1=1
    order by (select storagecode from a073 where a074.inStorageCode = a073.inStorageCode ) desc
    --a073 和 a074 一对多关系,或一对一关系
    --只能查一个字段排序
      

  5.   

    用exec() 执行拼接字符串的sql语句
      

  6.   

    动态语句实现   点什么按钮传一个对应的排序字段进来'select * from tb order by'+@col+'desc|asc'
      

  7.   


    一、按钮,它其实是一个链接二、假设有2个排序参数
    按钮1:参数A——0,参数B——time
    按钮2:参数A——name,参数B——0三、页面使用同一个存储过程,都传入参数A和参数B四、存储过程中
    if 参数A=0 then
    SQL语句
    end ifif 参数B=0 then
    SQL语句
    end if——上面仅仅是思路,语法是错误的。
      

  8.   


    要根据其他表来排序的话,比如 tableB 的time 字段, 则必须改动 SELECT * FROM tableA WHERE 为 SELECT tableA.* FROM tableA LEFT JOIN tableB ON tableA.Link_Col=tableB.Link_Col WHERE -- 红色部分根据你的具体需要作必要修改