两张表A,B结构分别如下:
A: id  A1 A2  B1
B: id  B1 B2
要求查出A中所有的记录并按照 B2的升序排列!

解决方案 »

  1.   

    最好性能高点的SQL,因为数据量很大
      

  2.   

    你的问题有点难以理解吧!是不是这样 查询出B表中包含A表数据的记录啊,以B表中的B2排序啊?  B表ID的外键是参照A表的ID吗?
      

  3.   

    哦!看错题目了! 呵呵! 原来你要查询A表的数据, A表和B表有什么关系呢?  如果是ID关联的话这个查询不难哦! 用 EXISTS 查询很快的! 
      

  4.   

    select A.A1 A.A2 A.B1 from A inner join B on A.B1=B.B1 order by B.B2 ASC/ select * from A left join B on A.B1 = B.B1 order by B.B2 asc
      

  5.   

    select A.A1 A.A2 A.B1 from A inner join B on A.B1=B.B1 order by B.B2 ASC/ select * from A left join B on A.B1 = B.B1 order by B.B2 asc
      

  6.   


    select a.*
    from a,b
    where a.id=b.id
    order by b.b2 asc;
      

  7.   

    是ID  还是 B1关联 LZ没说呢?
      

  8.   


    SQL> with b as(
      2     select 4 id,to_date('2011-04-01','yyyy-mm-dd') dt from dual union all
      3     select 3,to_date('2005-12-25','yyyy-mm-dd') from dual union all
      4     select 5,to_date('2010-05-21','yyyy-mm-dd') from dual union all
      5     select 1,to_date('2014-01-01','yyyy-mm-dd') from dual)
      6     select * from b
      7     order by dt asc
      8  /        ID DT
    ---------- -----------
             3 2005-12-25
             5 2010-05-21
             4 2011-04-01
             1 2014-01-01
    --
    select *
    from a,b
    where a.id=b.id
    order by b.dt asc;        ID A1         A2               ID DT
    ---------- ---------- -------- ---------- -----------
             3 china      america           3 2005-12-25
             4 CF         DNF               4 2011-04-01
             1 bbc        cnn               1 2014-01-01
    --
    select a.*
    from a,b
    where a.id=b.id
    order by b.dt asc;        ID A1         A2
    ---------- ---------- --------
             3 china      america
             4 CF         DNF
             1 bbc        cnn
      

  9.   


    SQL> with a as(
      2       select 1 id,'bbc' a1,'cnn' a2 from dual union all
      3       select 2,'basketball','football' from dual union all
      4       select 3,'china','america' from dual union all
      5       select 4,'CF','DNF' from dual)
      6  ,b as(
      7     select 4 id,to_date('2011-04-01','yyyy-mm-dd') dt from dual union all
      8     select 3,to_date('2005-12-25','yyyy-mm-dd') from dual union all
      9     select 5,to_date('2010-05-21','yyyy-mm-dd') from dual union all
     10     select 1,to_date('2014-01-01','yyyy-mm-dd') from dual)
     11  select a.*
     12  from a,b
     13  where a.id=b.id
     14  order by b.dt asc;        ID A1         A2
    ---------- ---------- --------
             3 china      america
             4 CF         DNF
             1 bbc        cnn