当一个字段为null时,左外连接

时间:2013-09-19 09:36:41

标签: php mysql

我有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

有时t3id在t3表中没有显示我需要结果名称,日期,如何在单个查询中?

1 个答案:

答案 0 :(得分: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