select p.name + '-' + c.name name from xdjd_Province p, xdjd_City c where p.code='210000' and c.code ='130600',
这样的话有个问题,如果p.code='0000000',这样不存在的省代码时候,这个数据就出不来,我希望的是,出不来的话,可以 -广州,城市出不来的话,广东省- 后面就是空
2条都为空的话,是 -,这2个表没有任何关联,各查各的,就是把2个结果合并下
select p.name + '-' + c.name name from xdjd_Province p outer join xdjd_City c where p.code='210000' and c.code ='130600'
试下看,有语法问题适当修正下。
结合case when 处理
select case p.name when '000000' then ' ' else p.name end
+'-'+case c.name when '000000' then ' ' else c.name end
name from xdjd_Province p, xdjd_City c where p.code='210000' or c.code ='130600' or p.code='000000' and c.code='000000'
试试看
declare @Pname nvarchar(20) select @Pname = p.name from xdjd_Province p where p.code = '000000' declare @Cname nvarchar(20) select @Cname = c.name from xdjd_City c where c.code = '130600' select ISNULL(@Pname, '') + ' - ' + ISNULL(@Cname, '')