【MySQL】mysqlleftjoin多个表参考:
SQL:
select * from t_a a left join t_b b on a.bid = b.bid left join t_c c on a.cid = c.cid
t_a表
t_b表
t_c表
总的结果:
============================================================== t_a表
t_b表
t_c表
总的结果表:
第⼆次带数据的sql:
Java代码
1. # MySQL-Front 3.2  (Build 14.3)
2.
3. /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
4. /*!40103 SET TIME_ZONE=’SYSTEM’ */;
5. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */;
6. /*!40101 SET SQL_MODE=’STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’ */;
7. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES */;
8. /*!40103 SET SQL_NOTES=’ON’ */;
9.
10.
11. # Host: 127.0.0.1    Database: jointest
12. # ——————————————————
13. # Server version 5.0.22-community-nt
14.
15. DROP DATABASE IF EXISTS `jointest`;
15. DROP DATABASE IF EXISTS `jointest`;
16. CREATE DATABASE `jointest` /*!40100 DEFAULT CHARACTER SET utf8 */;
17. USE `jointest`;
18.
19. #
20. # Table structure for table t_a
21. #
22.
23. CREATE TABLE `t_a` (
24.  `aid` int(11) NOT NULL auto_increment,
25.  `aname` varchar(11) default NULL,
26.  `bid` int(11) default NULL COMMENT ‘引⽤⾃t_b的bid’,
27.  `cid` int(11) default NULL COMMENT ‘引⽤⾃t_c的cid’,
28.  PRIMARY KEY  (`aid`)
29. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
30.
31. #
32. # Dumping data for table t_a
33. #
34.
35. INSERT INTO `t_a` VALUES (1,‘a名字1’,1,1);
36. INSERT INTO `t_a` VALUES (2,‘a名字2’,NULL,2);
37.
38. #
39. # Table structure for table t_b
40. #
41.
42. CREATE TABLE `t_b` (
sql left join 多表连接43.  `bid` int(11) NOT NULL auto_increment,
44.  `bname` varchar(11) default NULL,
45.  PRIMARY KEY  (`bid`)
46. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
47.
48. #
49. # Dumping data for table t_b
50. #
51.
52. INSERT INTO `t_b` VALUES (1,‘b名字’);
53.
54. #
55. # Table structure for table t_c
56. #
57.
57.
58. CREATE TABLE `t_c` (
59.  `cid` int(11) NOT NULL auto_increment,
60.  `cname` varchar(11) default NULL,
61.  PRIMARY KEY  (`cid`)
62. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
63.
64. #
65. # Dumping data for table t_c
66. #
67.
68. INSERT INTO `t_c` VALUES (1,‘c名字’);
69. INSERT INTO `t_c` VALUES (2,‘c名字2’);
70.
71. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
72. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
73. /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。