SQL:带有外键的交叉表到两个不同的表

发布于 2024-12-01 17:14:06 字数 704 浏览 2 评论 0原文

我有三个表:

zip_code_data
|zipCodeId| primary key
|zipCode| indexed
|other columns...|

location_data
|locationDataId| primary key
|city| indexed
|other columns...|

x_data
|id| primary key
|zipCodeId| foreign key
|locationDataId| foreign key

我的目标是对邮政编码或城市运行查询,并从 zip_code_datalocation_data 表中获取与其关联的所有数据
例如,如果用户搜索邮政编码,我想从两个表中提取与该邮政编码相关的所有数据。

我的第一个猜测是首先从交叉表中获取外键(x_data,下面的示例),然后使用它们从每个各自的表中获取数据......因为我有点对于新手用户,我不知道执行此操作的最佳方法。

SELECT x_data.zipCodeId, x_data.locationDataId
FROM x_data
INNER JOIN zip_code_data
ON x_data.zipCodeId=zip_code_data.zipCodeId
WHERE zip_code_data.zipCode LIKE '2322%'

I have three tables:

zip_code_data
|zipCodeId| primary key
|zipCode| indexed
|other columns...|

location_data
|locationDataId| primary key
|city| indexed
|other columns...|

x_data
|id| primary key
|zipCodeId| foreign key
|locationDataId| foreign key

My goal is to run a query for either zipcode or city, and get all of the data associated with it from the zip_code_data and location_data tables

For example, if a user searches for a zipcode, I want to pull back all of the data associated with that zipcode from both tables.

My first guess is to get the foreign keys first from the cross table (x_data, example below) and then use those to get the data from each respective table... Since i'm somewhat of a novice user I don't know the best way to do this.

SELECT x_data.zipCodeId, x_data.locationDataId
FROM x_data
INNER JOIN zip_code_data
ON x_data.zipCodeId=zip_code_data.zipCodeId
WHERE zip_code_data.zipCode LIKE '2322%'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-12-08 17:14:06

您可以创建内联视图:

 select zips.othercolumn, LOCS.city
      from zips
           inner join x_data on zips.zip = x_data_zip and zips.zip like .....

           left join (
                select id, locations.city from locations
                where locations.id = x_data.locationid
           ) as LOCS

或者只是加入位置表:

           left join locations as locs on locs.locationid = x_data.locationid

You could create an inline view:

 select zips.othercolumn, LOCS.city
      from zips
           inner join x_data on zips.zip = x_data_zip and zips.zip like .....

           left join (
                select id, locations.city from locations
                where locations.id = x_data.locationid
           ) as LOCS

or just join the locations table:

           left join locations as locs on locs.locationid = x_data.locationid
紫竹語嫣☆ 2024-12-08 17:14:06

我正要发帖:

SELECT zip.*,loc.*
FROM x_data xref
JOIN zip_code_data zip ON zip.zipCodeId=xref.zipCodeID
JOIN location_Data loc ON loc.locationDataID=xRef.locationDataID
WHERE zip.zipCode LIKE '2322%' or loc.city LIKE '%aaa%'

但看起来你已经得到了......

I was about to post:

SELECT zip.*,loc.*
FROM x_data xref
JOIN zip_code_data zip ON zip.zipCodeId=xref.zipCodeID
JOIN location_Data loc ON loc.locationDataID=xRef.locationDataID
WHERE zip.zipCode LIKE '2322%' or loc.city LIKE '%aaa%'

but it looks like you've already got it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文