insert into t_user values(1,'use1','123','user1',1,0,'test@qq.com',0,now());
insert into t_user values(2,'use2','123','user2',1,0,'test@qq.com',0,now());
insert into t_message values(1,'msg11','content11',now(),1);
insert into t_message values(2,'msg12','content12',now(),1);
insert into t_message values(3,'msg21','content21',now(),1);
insert into t_message values(4,'msg22','content22',now(),1);
insert into t_attachment values (1,'attach1','attach1','zip',now(),1);
insert into t_attachment values (2,'attach2','attach1','zip',now(),1);
select t_message.m_id,a_id from t_message,t_attachment where t_message.m_id = t_attachment.m_id;
select t_message.u_id,m_id from t_user,t_message where t_message.u_id = t_user.u_id;
#left join可以满足你的要求,右侧对附件表的连接,没有附件的话会补NULL
select * from ((select t_message.u_id,m_id from t_user,t_message where t_message.u_id = t_user.u_id) as a left join (select t_message.m_id,a_id from t_message,t_attachment where t_message.m_id = t_attachment.m_id) as b on a.m_id = b.m_id);
结果集:
u_id | m_id | m_id | a_id
——+——+——+——
1 | 1 | 1 | 1
1 | 1 | 1 | 2
1 | 2 | |
1 | 3 | |
1 | 4 | |
【使用left join连接带NULL值数据表案例】(5 rows)
推荐阅读
- 数据库|SQL行转列方式优化查询性能实践
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- SQL|SQL基本功(五)--函数、谓词、CASE表达式
- SQL|SQL基本功(三)-- 聚合与排序
- web挖洞|HACK学习黑帽子Python--漏洞检测脚本快速编写
- sqlite|python中用SQLite3添加 主键约束 唯一约束 非空约束 外键约束(约束的介绍以及设置)
- python|python中使用SQLite3对数据库的基本操作(基于ubuntu操作系统)
- TDSQL | DTS for PostgreSQL 逻辑复制详解
- TDSQL | 《checkpoint 原理浅析》
- TDSQL-A 技术架构演进及创新实践