一个表中的两个字段,关联另一个表中的一个字段,求查询解决Select TB_ICKaBiao.ID,TB_ICKaBiao.ICKaHao,TB_
一个表中的两个字段,关联另一个表中的一个字段,求查询解决
Select TB_ICKaBiao.ID,TB_ICKaBiao.ICKaHao,TB_ICKaBiao.ICKaLeiXing,TB_ICKaBiao.ICKaZhiKaRiQi,
ICKaZhuangTai,TB_ICKaBiao.Remark ,dbo.TB_SheDingShuJuBiao.Shedingshuju as ickl,TB_SheDingShuJuBiao.Shedingshuju as zhuangt
From TB_ICKaBiao,TB_SheDingShuJuBiao
Where TB_ICKaBiao.ICKaLeiXing=TB_SheDingShuJuBiao.ID or TB_ICKaBiao.ICKaZhuangTai=TB_SheDingShuJuBiao.ID
查询结果是两条,求合并成一条!
1B0012522012-01-01 00:00:00.000254NULL固定卡固定卡
1B0012522012-01-01 00:00:00.000254NULL空置空置 多表关联查询
[解决办法]上面那位大侠代码改进下就可以了
SELECT a.ID,a.ICKaHao,a.ICKaLeiXing,a.ICKaZhiKaRiQi,a.ICKaZhuangTai,a.Remark , b.Shedingshuju AS ickl ,b.Shedingshuju AS zhuangt
FROM TB_ICKaBiao a CROSS APPLY (
SELECT TOP(1) *
FROM TB_SheDingShuJuBiao m
WHERE (m.ID =a.ICKaLeiXing OR m.ID = a.ICKaZhuangTai) and m.Shedingshuju<>a.Shedingshuju and m.Shedingshuju<>a.Shedingshuju
ORDER BY (CASE WHEN m.ID = a.ICKaLeiXing THEN 0 ELSE 1 END)
) b