mysql> select * from D_DEVICE_SOFT_VERSION
-> ;
+-------+---------+--------------+--------+
| verid | vername | devicetypeid | status |
+-------+---------+--------------+--------+
| 2 | 1.0 | 2 | 0 |
+-------+---------+--------------+--------+
1 row in set (0.00 sec)
mysql> select * from D_DEVICE_TYPE;
+--------------+----------------+-------------+--------+-------------+
| devicetypeid | devicetypename | manufacture | status | description |
+--------------+----------------+-------------+--------+-------------+
| 2 | 类型一 | 厂商一 | 0 | |
+--------------+----------------+-------------+--------+-------------+
1 row in set (0.00 sec)
如上两个表含有不同类型的数据,但是又有相同的devicetypeid列,怎样将两个表的内容组合起来输出一个新的表数据呢。于是我想到了join。
join分为left join 和right join 中文就是左链接和右链接。
left join :所谓左链接就是以左表为主表,坐标的每行都要输出,不管右表是否含有表链接的关键值,都按照左表逐行输出,右表不存在的则右表输出null。
right join:和左连接相反。
用法举例,取出左边表的devicetypeid和devicetypename,通过devicetypeid链接并取出右表的vername。
mysql> select a.devicetypename,a.manufacture,b.vername from D_DEVICE_TYPE a left join D_DEVICE_SOFT_VERSION b on a.devicetypeid=b.devicetypeid; +----------------+-------------+---------+
| devicetypename | manufacture | vername |
+----------------+-------------+---------+
| 类型一 | 厂商一 | 1.0 |
+----------------+-------------+---------+
1 row in set (0.00 sec)