首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于大类和二类的联合查询,部分大类下二类为空怎么处理

2012-02-13 
关于大类和二类的联合查询,部分大类下二类为空怎么办?大类表:ClassTable字段: cid,className(大类的id以及

关于大类和二类的联合查询,部分大类下二类为空怎么办?
大类表:ClassTable
字段: cid,className(大类的id以及名称)
二类表:SortTable
字段:sid,sortName,clsName(二类的id、二类名称以及所属大类名称)
不是所有的大类下都有二类。怎么样实现如下效果?(没有二类的大类名称也显示)
(我用 select classname,sortname from classTable join sortTable on classname=clsname 只显示有二类的内容)
大类 二类
蔬菜 菠菜
蔬菜 韭菜
水果 苹果
水果 香蕉
水果 橘子
粮食
家禽 鸡
家禽 鸭
水产
副食

[解决办法]

SQL code
create table t1(    id int,    name varchar(10))insert into t1select 1,'蔬菜' union allselect 2,'水果' union allselect 3,'粮食' union allselect 4,'家禽' union allselect 5,'水产' union allselect 5,'副食'create table t2(    id int,    name varchar(10),    pname varchar(10))insert into t2select 1,'菠菜','蔬菜' union allselect 1,'韭菜','蔬菜' union allselect 1,'苹果','水果' union allselect 1,'香蕉','水果' union allselect 1,'橘子','水果' union allselect 1,'鸡','家禽' union allselect 1,'鸭','家禽'select * from t1select * from t2select t1.name,isnull(t2.pname,'') as pname from t1 left join t2 on t1.name=t2.pname-----------------------name    pname蔬菜    蔬菜蔬菜    蔬菜水果    水果水果    水果水果    水果粮食    家禽    家禽家禽    家禽水产    副食 

热点排行