表n:国家
id,nn
1 中国
2 美国
表c:城市
id,ct,nid
1 北京 1
2 上海 1
3 华盛顿 2
如何用一条语句循环显示结果:
中国
北京 上海
美国
华盛顿
按以前的办法是:
select * from n
do while not n.eof
  select * from c where c(nid)=n(id)
  do while not c.eof
这样两次循环查询也可以得出以上结果,但有没有一条语句一个循环得出以上结果呢?请教了

解决方案 »

  1.   

    step1: 将表c的数据输出为
    1|北京,上海
    2|华盛顿
    (可以在csdn上找答案)
    step2: 将上面的结果与表n做join
    就得到你要的结果了。
      

  2.   

    city还是横着的,不确定个数的?一行显示全部city的?
      

  3.   

    結果是不是格式貼錯了 
    select 
    n.nn,c.ct
    from 
    c
    join 
    n on c.ID=n.nid
      

  4.   

    select 表n.nn,表c.ct from 表c inner join 表n on 表c.nid=表n.id
      

  5.   

    中国 
         北京 
         上海 
    美国  
        华盛顿 是不是这样的格式啊??
    如果像你贴的
    中国 
    北京 上海 
    美国 
    华盛顿 用sql做进来就不简单了,因为你city不固定,列就不固定
      

  6.   


    --> 测试数据: [表n]
    if object_id('[表n]') is not null drop table [表n]
    create table [表n] (id int,nn varchar(4))
    insert into [表n]
    select 1,'中国' union all
    select 2,'美国'--> 测试数据: [表c]
    if object_id('[表c]') is not null drop table [表c]
    create table [表c] (id int,ct varchar(6),nid int)
    insert into [表c]
    select 1,'北京',1 union all
    select 2,'上海',1 union all
    select 3,'华盛顿',2
    select nn,ct from
    (
    select nn,''as ct,id as id from 表n
    union all
    (
    select '' as nn,b.ct,a.id as id from 表n as a inner join 表c as b on a.id=b.nid
    )
    ) as a order by a.id,a.ct
    /*
    中国
    北京
    上海
    美国
    华盛顿
    */
      

  7.   


    if not object_id('n') is null
    drop table n
    Go
    Create table n([id] int,[nn] nvarchar(2))
    Insert n
    select 1,N'中国' union all
    select 2,N'美国'
    Go
    if not object_id('c') is null
    drop table c
    Go
    Create table c([id] int,[ct] nvarchar(3),[nid] int)
    Insert c
    select 1,N'北京',1 union all
    select 2,N'上海',1 union all
    select 3,N'华盛顿',2
    Gocreate function F_str(@nid int)
    returns nvarchar(100)
    as
    begin
    declare @s nvarchar(100) 
    select @s=isnull(@s+' ','')+ct from c where nid=@nid
    return @s
    endgo
    select ID ,nn
    from
    (select * ,0 as ord from n
    union all
    select distinct nid,dbo.F_str(nid),1 ord from c )T
    order by ID,ord
    ID          nn
    ----------- ----------------------------------------------------------------------------------------------------
    1           中国
    1           北京 上海
    2           美国
    2           华盛顿(4 個資料列受到影響)
      

  8.   

    中国 
        北京  
        上海 
    美国  
        华盛顿 
    对,就这个格式,结果出来了,如何循环?select 表n.nn,表c.ct from 表c inner join 表n on 表c.nid=表n.id
    结果:
    北京 中国
    上海 中国
    华盛顿 美国
    结果列出后,如何循环?
      

  9.   

    查询出结果后用ASP语句如何循环?
      

  10.   

    asp就一个do while就行了啊
    你什么意思? 
      

  11.   

    不好意思,可能是我发错地方了,我是想用在网页上显示出来的。谢谢roy_88中国风,SQL查询分析可以有你的结果
      

  12.   

    create table ta(id int,nn varchar(20))
    insert into ta select 1,'中国'
    insert into ta select 2,'美国'create table tb(id int,ct varchar(50),nid int)
    insert into tb select 1,'北京',1
    insert into tb select 2,'上海',1
    insert into tb select 3,'华盛顿',2select * from (
    select nn,'' as ct from ta
    union all
    select nn,ct
    from ta a left join tb b on a.id=b.nid) t
    order by nn,case when ct='' then 0 else 1 endnn ct
    美国
    美国 华盛顿
    中国
    中国 北京
    中国 上海
      

  13.   

    perfectaction 
    完美行动
    do while 如何写呀?帮忙就这种显示:
    中国 
        北京  
        上海 
      天津
    美国  
        华盛顿
      纽约
    日本
      东京
      

  14.   

    create table ta(id int,nn varchar(20))
    insert into ta select 1,'中国'
    insert into ta select 2,'美国'create table tb(id int,ct varchar(50),nid int)
    insert into tb select 1,'北京',1
    insert into tb select 2,'上海',1
    insert into tb select 3,'华盛顿',2select case when ct ='' then nn else '' end as nn ,
    ct
    from (
    select nn,'' as ct,nn as nn2 from ta
    union all
    select nn,ct,nn as nn2
    from ta a left join tb b on a.id=b.nid) t
    order by nn2,case when ct='' then 0 else 1 endnn ct
    美国
    华盛顿
    中国
    北京
    上海