1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 1. 内连接:Inner join SELECT LEFT(a.title, 20) AS ArticleTitle, LEFT(c.content, 20) AS CommentContent FROM article a INNER JOIN comment c ON a.id = c.article_id LIMIT 2; 返回左右均匹配的行
2. 外连接:LEFT JOIN RIGHT JOIN
(1). 左连接 SELECT LEFT(a.title, 20) AS ArticleTitle, LEFT(c.content, 20) AS CommentContent FROM article a LEFT JOIN comment c ON a.id = c.article_id LIMIT 2; 会返回左边的全部行,右边无匹配行则返回null
(2). 右连接 SELECT LEFT(a.title, 20) AS ArticleTitle, RIGHT(c.content, 20) AS CommentContent FROM comment c RIGHT JOIN article a ON a.id = c.article_id LIMIT 2; 会返回右边的全部行,左边无匹配行则返回null
|