1. 程式人生 > 實用技巧 >MySQL資料庫之多表查詢cross join交叉連線

MySQL資料庫之多表查詢cross join交叉連線

交叉連線

  • 語法
    • select * from 表1 cross join 表2 on ...
    • 交叉連線如果沒有連線條件返回笛卡爾積
    • 如果有連線條件和內連線是一樣的
MariaDB [sel]> select * from grades cross join resume;
+-------+---------+------+----+-------+-----------+
| name  | chinese | math | id | name  | skill     |
+-------+---------+------+----+-------+-----------+
| Sunny |      93 |   96 |  1 | Sunny | php       |
| Sunny |      93 |   96 |  2 | Kimmy | php       |
| Sunny |      93 |   96 |  3 | Jerry | php,mysql |
| Jerry |      97 |   91 |  1 | Sunny | php       |
| Jerry |      97 |   91 |  2 | Kimmy | php       |
| Jerry |      97 |   91 |  3 | Jerry | php,mysql |
| Marry |      95 |   94 |  1 | Sunny | php       |
| Marry |      95 |   94 |  2 | Kimmy | php       |
| Marry |      95 |   94 |  3 | Jerry | php,mysql |
| Tommy |      98 |   94 |  1 | Sunny | php       |
| Tommy |      98 |   94 |  2 | Kimmy | php       |
| Tommy |      98 |   94 |  3 | Jerry | php,mysql |
+-------+---------+------+----+-------+-----------+
# `12 rows in set (0.001 sec)`
  • 交叉連線有連線表示式與內連線是一樣的
MariaDB [sel]> select * from grades cross join resume on grades.name=resume.name;
+-------+---------+------+----+-------+-----------+
| name  | chinese | math | id | name  | skill     |
+-------+---------+------+----+-------+-----------+
| Sunny |      93 |   96 |  1 | Sunny | php       |
| Jerry |      97 |   91 |  3 | Jerry | php,mysql |
+-------+---------+------+----+-------+-----------+
# `2 rows in set (0.000 sec)`