mysql左连接

举个例子说明:

select d.id, d.uid,d.username,d.dateline, d.message,d.replynum, c.picid, c.filename from doing d left join(

        select a.*, b.filename from picture a left join my_picture b on (a.id = b.id and a.mid = b.mid)

) c

on c.doid = d.doid limit 1, 20;

 

解析: 左连接是左边的表数据全部保留,右边的表选择符合条件的数据,拼接成一个数据集(表)。

select a.*, b.filename from picture a left join my_picture b on (a.id = b.id and a.mid = b.mid)这句选取出来的当做一个c表。

 

c表和d表作左连接,相当于:

select d.id, d.uid,d.username,d.dateline, d.message,d.replynum, c.picid, c.filename from doing d left join on c.doid = d.doid limit 1, 20;

 

多个表左连接一样,不断的接下去就好了,如下:

select * from
t4 left join t1 on t4.a=t1.a
    left join t2 on t4.b=t2.b and t1.id=t2.id
    left join t3 on t4.c=t3.c and t2.id=t3.id

你可能感兴趣的:(mysql)