首页 > 解决方案 > 我们如何创建一个视图,其中表 A 中的 2 个字段都需要表 B 中特定字段的数据?

问题描述

创建一个名为 TripMaps 的新 SQL 视图,其中包括每个 TripID 的起点和终点车站的车站详细信息(地标、纬度、经度)。(Stations.name 和 trips.startstation 包含相似的站名)

在此处输入图像描述

标签: mysql

解决方案


create or replace view TripMaps as
select
s.Name,
s.CoordLat,
s.CoordLong,
s.Landmark
from Stations s
where s.Name in (Select t.StartStation,t.EndStation from Trips t);

这假设您为 Stations.Name 和 Trips.StartStation / Trips.EndStation 使用具有相同格式的完全相同的名称)


推荐阅读