mysql的左连接应用
生活随笔
收集整理的這篇文章主要介紹了
mysql的左连接应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
如果兩個表存在外鍵連接,可根據(jù)外鍵對兩個表進行左連接,這時候外鍵相對應的其他列如果有NULL值,代表這兩個表可能存在不同步的現(xiàn)象,可刪除相應的值使2個表同步。 操作實例:
mysql> desc branch;
+-------+-------------+------+-----+---------+-------+
| Field | Type??????? | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| bid?? | int(4)????? | NO?? | PRI |???????? |?????? |
| cid?? | int(3)????? | NO?? |???? |???????? |?????? |
| bdesc | text??????? | NO?? |???? |???????? |?????? |
| bloc? | varchar(25) | YES? |???? | NULL??? |?????? |
+-------+-------------+------+-----+---------+-------+ mysql> desc client;
+-------+--------+------+-----+---------+----------------+
| Field | Type?? | Null | Key | Default | Extra????????? |
+-------+--------+------+-----+---------+----------------+
| cid?? | int(3) | NO?? | PRI | NULL??? | auto_increment |
| cname | text?? | NO?? |???? |???????? |??????????????? |
+-------+--------+------+-----+---------+----------------+
2 rows in set (0.01 sec) mysql> select client.cid,client.cname,branch.bid,branch.bdesc from client left join branch using (cid);
+-----+-------+------+---------------+
| cid | cname | bid? | bdesc???????? |
+-----+-------+------+---------------+
| 101 | WANG? | 1011 | cororate hq?? |
| 101 | WANG? | 1012 | cDDororate hq |
| 101 | WANG? | 1013 | DDrate hq???? |
| 102 | JV??? | NULL | NULL????????? |
| 103 | dJV?? | 1015 | DDratDDhq???? |
| 103 | dJV?? | 1016 | DDratDDhDq??? |
| 104 | JAMES | 1014 | DDratDDhq???? |
| 105 | TOMES | NULL | NULL????????? |
+-----+-------+------+---------------+
8 rows in set (0.00 sec) mysql> delete from client where cid in(102,105);
Query OK, 2 rows affected (0.00 sec) mysql> select client.cid,client.cname,branch.bid,branch.bdesc from client left join branch using (cid);
+-----+-------+------+---------------+
| cid | cname | bid? | bdesc???????? |
+-----+-------+------+---------------+
| 101 | WANG? | 1011 | cororate hq?? |
| 101 | WANG? | 1012 | cDDororate hq |
| 101 | WANG? | 1013 | DDrate hq???? |
| 103 | dJV?? | 1015 | DDratDDhq???? |
| 103 | dJV?? | 1016 | DDratDDhDq??? |
| 104 | JAMES | 1014 | DDratDDhq???? |
+-----+-------+------+---------------+
6 rows in set (0.00 sec)
mysql> desc branch;
+-------+-------------+------+-----+---------+-------+
| Field | Type??????? | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| bid?? | int(4)????? | NO?? | PRI |???????? |?????? |
| cid?? | int(3)????? | NO?? |???? |???????? |?????? |
| bdesc | text??????? | NO?? |???? |???????? |?????? |
| bloc? | varchar(25) | YES? |???? | NULL??? |?????? |
+-------+-------------+------+-----+---------+-------+ mysql> desc client;
+-------+--------+------+-----+---------+----------------+
| Field | Type?? | Null | Key | Default | Extra????????? |
+-------+--------+------+-----+---------+----------------+
| cid?? | int(3) | NO?? | PRI | NULL??? | auto_increment |
| cname | text?? | NO?? |???? |???????? |??????????????? |
+-------+--------+------+-----+---------+----------------+
2 rows in set (0.01 sec) mysql> select client.cid,client.cname,branch.bid,branch.bdesc from client left join branch using (cid);
+-----+-------+------+---------------+
| cid | cname | bid? | bdesc???????? |
+-----+-------+------+---------------+
| 101 | WANG? | 1011 | cororate hq?? |
| 101 | WANG? | 1012 | cDDororate hq |
| 101 | WANG? | 1013 | DDrate hq???? |
| 102 | JV??? | NULL | NULL????????? |
| 103 | dJV?? | 1015 | DDratDDhq???? |
| 103 | dJV?? | 1016 | DDratDDhDq??? |
| 104 | JAMES | 1014 | DDratDDhq???? |
| 105 | TOMES | NULL | NULL????????? |
+-----+-------+------+---------------+
8 rows in set (0.00 sec) mysql> delete from client where cid in(102,105);
Query OK, 2 rows affected (0.00 sec) mysql> select client.cid,client.cname,branch.bid,branch.bdesc from client left join branch using (cid);
+-----+-------+------+---------------+
| cid | cname | bid? | bdesc???????? |
+-----+-------+------+---------------+
| 101 | WANG? | 1011 | cororate hq?? |
| 101 | WANG? | 1012 | cDDororate hq |
| 101 | WANG? | 1013 | DDrate hq???? |
| 103 | dJV?? | 1015 | DDratDDhq???? |
| 103 | dJV?? | 1016 | DDratDDhDq??? |
| 104 | JAMES | 1014 | DDratDDhq???? |
+-----+-------+------+---------------+
6 rows in set (0.00 sec)
轉(zhuǎn)載于:https://blog.51cto.com/dadloveu/197238
總結(jié)
以上是生活随笔為你收集整理的mysql的左连接应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 转义字符是什么
- 下一篇: SEO的工作职责就是让网站拥有更多的关键