马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
接下来我们就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一样)来连接以上两张表来读取runoob_tbl表中所有runoob_author字段在tcount_tbl表对应的runoob_count字段值: mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;+-----------+---------------+--------------+| runoob_id | runoob_author | runoob_count |+-----------+---------------+--------------+| 1 | John Poul | 1 || 3 | Sanjay | 1 |+-----------+---------------+--------------+2 rows in set (0.00 sec)以上 SQL 语句等价于: mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author;+-------------+-----------------+----------------+| runoob_id | runoob_author | runoob_count |+-------------+-----------------+----------------+| 1 | John Poul | 1 || 3 | Sanjay | 1 |+-------------+-----------------+----------------+2 rows in set (0.01 sec)mysql> MySQL LEFT JOINMySQL left join 与 join 有所不同。 MySQL LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据。 实例尝试以下实例,以 runoob_tbl 为左表,tcount_tbl 为右表,理解MySQL LEFT JOIN的应用: root@host# mysql -u root -p password;Enter password:*******mysql> use RUNOOB;Database changedmysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a LEFT JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;+-------------+-----------------+----------------+| runoob_id | runoob_author | runoob_count |+-------------+-----------------+----------------+| 1 | John Poul | 1 || 2 | Abdul S | NULL || 3 | Sanjay | 1 |+-------------+-----------------+----------------+3 rows in set (0.02 sec)以上实例中使用了LEFT JOIN,该语句会读取左边的数据表runoob_tbl的所有选取的字段数据,即便在右侧表tcount_tbl中没有对应的runoob_author字段值。
|