有三个表,user, camera, uc,如何写一个sql语句,使得能通过uc表现是相对用户所能查看的camera?camera
id cameraname
1 cam1
2 cam2
3 cam3
4 cam4
5 cam5
6 cam6
7 cam7
8 cam8user
id username
1 bryan
2 tonyuc
id uid cid
1 2 3
2 2 4
3 1 1
4 1 5
5 1 6

解决方案 »

  1.   

    select
        a.id,
        b.username,
        c.cameraname
    from uc as a
       join [user] as b
    on a.uid=b.id
       join [camera] as c
    on a.cid=c.id
      

  2.   

    select c.id,a.username,b.cameraname  from user a,camera b,uc c where a.id=c.uid and b.id=c.cid
      

  3.   

    select
        a.id,
        b.username,
        c.cameraname
    from uc as a,
       [user] as b,
       [camera] as c
    where
       a.uid=b.id
    and 
    a.cid=c.id
      

  4.   

    select 
      c.id,
      b.username,
      a.cameraname
    from
      uc c
    left join [user] b on c.uid=b.id
    left join camera a on c.cid=a.id
      

  5.   

    我要的效果是:当我要显示tony能控制的camera的时候,他会查找uc表,对应tony的userid查找到他能控制的cameraid list ,并list显示出来
      

  6.   

    select B.username,C.cameraname from uc A inner join [user] B
    On A.uid=B.id inner join camera C on A.cid=c.id
      

  7.   

    最终我要显示的是camera list
      

  8.   

    select a.* from camera a,user b,uc c
    where a.id=c.cid and b.id=c.uid and b.username='你要查找的用户'--如tony
    select a.* from camera a,user b,uc c
    where a.id=c.cid and b.id=c.uid and b.username='tony'
      

  9.   


    declare @a varchar(8)
    select @a='Tony' --自己定义用户
    select
        a.id,
        b.username,
        c.cameraname
    from uc as a,
       [user] as b,
       [camera] as c
    where
       a.uid=b.id and a.cid=c.id and b.username=@a
      

  10.   


    select camera.* from camera
      inner join UC on UC.cid = camera.id
      inner join [user] on [user].id = UC.uid
      where [user].[id]=''
      

  11.   


    select camera.* from camera
      inner join UC on UC.cid = camera.id
      inner join [user] on [user].id = UC.uid
      where [user].[username]= ?
    --orselect c.* from 
    camera c,uc uc,user u
    where u.id = uc.uid and uc.cid = c.cid
    and user.username = ?