mysql有表A:id   num
1    4
2    9
3    14
4    20
5    25
表B:id start end
1  0     5
2  6     10
3  11    15怎么能查询到存在于表B中任一的start字段和end字段之间的数?比如 表A中的4,9,14都是在表b两个字段之间的数,而20和25因为没有在表b任一字段之间所以无法查询出来..

解决方案 »

  1.   

    select a.* from a inner join b
    on a.num between b.start and b.enf
      

  2.   

    mysql> select * from a;
    +----+------+
    | id | num  |
    +----+------+
    |  1 |    4 |
    |  2 |    9 |
    |  3 |   14 |
    |  4 |   20 |
    |  5 |   25 |
    +----+------+
    5 rows in set (0.00 sec)mysql> select * from b;
    +----+-------+------+
    | id | start | end  |
    +----+-------+------+
    |  1 |     0 |    5 |
    |  2 |     6 |   10 |
    |  3 |    11 |   15 |
    +----+-------+------+
    3 rows in set (0.00 sec)mysql> select a.id,num from a,b where a.id=b.id and a.num between b
        -> .start and b.end;
    +----+------+
    | id | num  |
    +----+------+
    |  1 |    4 |
    |  2 |    9 |
    |  3 |   14 |
    +----+------+
    3 rows in set (0.02 sec)
      

  3.   

    select a.*
    from a inner jon b on a.num between b.start and b.end
    select * from a
    where exists (select 1 from b where a.num  between b.start and b.end)
      

  4.   


    你好,问下MySql5.1的MyISAM引擎数据库支持子查询吗?
    我前几天写了个报错。