简体   繁体   English

关系表MYSQL

[英]Relational tables MYSQL

I have two tables in a database. 我在数据库中有两个表。 One is the name of a provider (name, general info) the other is the type of provider. 一个是提供者的名称(名称,一般信息),另一个是提供者的类型。 The provider can be more than one type. 提供者可以是一种以上的类型。 So in my provider database you can choose three different types they would be. 因此,在我的提供者数据库中,您可以选择三种不同的类型。

I am having trouble figuring out how to access the provider_type name. 我在弄清楚如何访问provider_type名称时遇到了麻烦。 I assume a join but can't seem to get past that. 我假设一个联接,但似乎无法超越。

This is what I have now: 这就是我现在拥有的:

SELECT p.name, p.phone, p.provider_type1, p.provider_type2 
FROM providers AS p 
INNER JOIN provider_type ON p.provider_type1 = provider_type.id
WHERE installation_id = $installation_id

The problem I keep running across is a provider that has more than one type they can be comes up in the query twice, rather I just want to list that provider then underneath list the type of services they can provide. 我一直在运行的问题是一个提供程序,它有多种类型,它们可以在查询中出现两次,而我只想列出该提供程序,然后列出它们可以提供的服务类型。

The join doesn't seem right. 联接似乎不正确。 All I need to do is grab the provider_type name with the same id as providers.provider_type1 etc.. 我需要做的就是获取provider_type名称,其名称与providers.provider_type1等相同。

What should I be doing instead. 我应该怎么做呢。 Thanks. 谢谢。

Join with provider twice, with two aliases, one for each provider_type. 与提供者两次连接,具有两个别名,每个提供者一个。

See this . 看到这个

(But better to have a table for rows "provider [provider_id] has type (name) [provider_type]" instead of the provider_typeX columns. Although that's not normalization, just good design.) (但最好有一个行表“provider [provider_id]有类型(名称)[provider_type]”而不是provider_typeX列。虽然这不是规范化,但只是好的设计。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM