sunlightcs 2010-12-29 21:20
浏览 217
已采纳

ibatis查询

原本要两条SQL语句的,我现在只想在一个里执行完,有什么办法吗?

[code="sql"]
select max(fans),min(fans) from tb_fans where userId=#userId#

select * from tb_article where userId bewteen (max) and (min)

其中max代表上面哪条SQL查询的最大值,min代表上面哪条SQL查询的最小值。

注:下面这种方法不行了,多执行了一条SQL语句。
select * from tb_article where userId bewteen (select max(fans) from tb_fans where userId=#userId#) and (select min(fans) from tb_fans where userId=#userId#)
[/code]

  • 写回答

3条回答

  • ageofnodoubt 2010-12-29 22:39
    关注

    MySQL数据库:
    [code="java"]
    方法一:
    select *
    from tb_article a,
    (select min(fans) as m, max(fans) as n from tb_fans where userId=#userId#) b
    where a.userId between b.m and b.n;

    方法二:
    select *
    from tb_article
    where userId between
    (select min(fans) from tb_fans where userId=#userId#) and
    (select max(fans) from tb_fans where userId=#userId#);
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗