用INNER JOIN 连接表的语法:
SELECT column, another_table_column, …
FROM mytable (主表)
INNER JOIN another_table (要连接的表)
ON mytable.id = another_table.id (想象一下刚才讲的主键连接,两个相同的连成1条)
WHERE condition(s)
ORDER BY column, … ASC/DESC
LIMIT num_limit OFFSET num_offset;
通过
ON
条件描述的关联关系;INNER JOIN
先将两个表数据连接到一起. 两个表中如果通过ID互相找不到的数据将会舍弃。
1. 【联表】找到所有电影的国内Domestic_sales
和国际销售额 ✓
select *
from movies
inner join Boxoffice
on movies.id=Boxoffice.movie_id
2. 【联表】找到所有国际销售额比国内销售大的电影
select *
from movies
inner join Boxoffice
on movies.id=Boxoffice.movie_id
where Domestic_sales < international_sales
3. 【联表】找出所有电影按市场占有率rating
倒序排列
select *
from movies
inner join Boxoffice
on movies.id=Boxoffice.movie_id
order by rating desc
4. 【联表】每部电影按国际销售额比较,排名最靠前的导演是谁,国际销量多少
select director,international_sales
from movies
inner join Boxoffice
on movies.id=Boxoffice.movie_id
order by international_sales desc
limit 1