3

我有 3 个表 t1、t2、t3,其字段如下所示

t1- t1id,name,age;
t2- t2id,t1id,date;
t3- t3id,t2id,time;

我的查询是

select concat(t1.name,',',t2.date,',',t3.time) 
from t1 
left outer join t2 on t1.t1id=t2.t1id 
left inner join t3 on t2.t2id=t3.t2id
where t1.age= 12

有时 t2id 没有出现在 t3 表中,我需要结果作为名称,日期,怎么可能在单个查询中?

4

1 回答 1

1

即使某些值是ifnull(),您也可以使用concat()null

select concat(t1.name, ',', ifnull(it2.date, ''), ',', ifnull(t3.time,'')) 
from t1 
left outer join t2 on t1.t1id=t2.t1id 
left inner join t3 on t2.t2id=t3.t2id 
where t1.age= 12

使用CONCAT_WS()

select concat_ws(',', t1.name, it2.date, t3.time) 
from t1 
left outer join t2 on t1.t1id=t2.t1id 
left inner join t3 on t2.t2id=t3.t2id 
where t1.age= 12
于 2013-09-19T09:39:42.293 回答