已知有A表
ID, Name 两字段,.关系表R
ID, A_ID, B_ID其中A_ID对应的是A表中的ID.求一VIEW的字段为
A_ID, A_NAME CountA_ID为A表中的ID,A_NAME 为A表中的Name, Count 为A_ID=某值时,在R表中的记录数.example:
Table A
ID Name
1  A1
2  A2
3  A3
4  A4Table R
ID A_ID  B_ID
1    1    1
2    1    2
3    1    3
4    2    2 
5    2    5
6    3    3
7    3    4View
A_ID  A_NAME  Count
1       A1       3
2       A2       2
3       A3       2

解决方案 »

  1.   

    给你个例子吧
    SQL>  select d.deptno,d.dname, t.empcount from dept d,
      2   (select deptno, count(*) empcount from emp group by deptno) t
      3   where d.deptno=t.deptno;    DEPTNO DNAME            EMPCOUNT
    ---------- -------------- ----------
            10 ACCOUNTING              3
            20 RESEARCH                5
            30 SALES                   6
      

  2.   

    create view V as(
    select A_id,name,count(R.id) from A,R where A.id = R.A_id group by A_id,name
    )
      

  3.   


    楼上都是正解,你要的是视图,楼上就可以select * from 视图     就OK了 
      

  4.   


    --直接来create view v_count
    as
    select a.id,a.name,count(*) 数量
    from a,r
    where a.id=r.a_id
    group by a.id,a.name