日期:2014-05-17  浏览次数:20719 次

请教一个sql语句的问题
select b.dicname as '样品类别'
from kcwms.enoutlist as a,kcwms.dictname as b
where b.dicid=a.sampletype and b.dictype='SAMPLE';

select b.dicname as '物品状态'
from kcwms.enoutlist as a,kcwms.dictname as b
where b.dicid=a.sampletype and b.dictype='STATE';
这两个sql语句合起来怎么写?
在线等。

------解决方案--------------------
LZ是要分组查询吗? 
直接用group by dictype就行了 

select b.dicname from kcwms.enoutlist as a,kcwms.dictname as b
where b.dicid=a.sampletype 
group by b.dictype


------解决方案--------------------
b.dicname 的别名有两个:'样品类别'和'物品状态'
要合起来的话,有难度……


如果两个别名一样的话就容易了:
SQL code

select b.dicname as '样品类别'
from kcwms.enoutlist as a,kcwms.dictname as b
where b.dicid=a.sampletype and b.dictype in ('SAMPLE','STATE');

------解决方案--------------------
LZ 需要的结果是几列? 

如果是两列 做不到你需要的结果 列名不可能变化 


------解决方案--------------------
select case when b.dictype='SAMPLE' then b.dicname else '' end as '样品类别'
,case when b.and b.dictype='STATE' then b.dicname else '' end as 物品状态'
from kcwms.enoutlist as a,kcwms.dictname as b
where b.dicid=a.sampletype ;

这样的话就产生了两个列 !楼主参照
------解决方案--------------------
探讨
select case when b.dictype='SAMPLE' then b.dicname else '' end as '样品类别'
,case when b.and b.dictype='STATE' then b.dicname else '' end as 物品状态'
from kcwms.enoutlist as a,kcwms.dictname as b
where……

------解决方案--------------------
SQL code

select '' as '物品状态',b.dicname as '样品类别'
from kcwms.enoutlist as a,kcwms.dictname as b
where b.dicid=a.sampletype and b.dictype='SAMPLE';
union all
select b.dicname as '物品状态','' as '样品类别'
from kcwms.enoutlist as a,kcwms.dictname as b
where b.dicid=a.sampletype and b.dictype='STATE';
是不是这个意思?


when case 也可以实现