简体   繁体   English

使用LEFT OUTER JOIN返回一个匹配的行和其他仅匹配的ID

[英]Using LEFT OUTER JOIN return one matched row and additional only matching id's

I have a large database that has about 190 columns in 22 tables. 我有一个大型数据库,在22个表中大约有190列。 There are a few tables that allow multiple entries into the database all of the values are referenced by a foreign key. 有一些表允许多个条目进入数据库,所有值都由外键引用。 When I use a LEFT OUTER JOIN If there are multiple entries in a single column matching a specific ID then it creates a new row with all of the information as before only changing the table fields. 当我使用LEFT OUTER JOIN如果单个列中有多个与特定ID匹配的条目,则它会像仅更改表字段之前一样,创建一个包含所有信息的新行。 For example: 例如:

+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| CompanyID | Name                      | Address                     | City           | State | Zip   | Country | Website                       |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
|       227 | Hello Company             | 123 blvd                    | Boom           | OK    | 56008 | USA     | www.imtired.com               |
|       228 | Test Company              | 87 Wesley Street            | Denham         | LA    | 21726 | USA     | www.tests.com                 |
|       229 | Testing Company           | 2 US hwy 281 N.             | Antonio        | TX    | 64258 | USA     | www.modeling.com              |
|       230 | TestCompany               | 45 W. 95th St               | Oak Lawn       | IL    | 61453 | USA     | www.express.com               |
|       235 | Encore                    | 2142 S. Patterson           | City           | IA    | 43106 | USA     | www.boomsite.com              |
|       235 | Encore                    | 2142 S. Patterson           | City           | IA    | 43106 | USA     | www.testingsite.com           |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+

You see that the company Encore has two rows with only the website being different is there a way to make it like this: 您会看到Encore公司有两行,只有网站不同,有没有办法做到这一点:

+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| CompanyID | Name                      | Address                     | City           | State | Zip   | Country | Website                       |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
|       227 | Hello Company             | 123 blvd                    | Boom           | OK    | 56008 | USA     | www.imtired.com               |
|       228 | Test Company              | 87 Wesley Street            | Denham         | LA    | 21726 | USA     | www.tests.com                 |
|       229 | Testing Company           | 2 US hwy 281 N.             | Antonio        | TX    | 64258 | USA     | www.modeling.com              |
|       230 | TestCompany               | 45 W. 95th St               | Oak Lawn       | IL    | 61453 | USA     | www.express.com               |
|       235 | Encore                    | 2142 S. Patterson           | City           | IA    | 43106 | USA     | www.boomsite.com              |
|           |                           |                             |                |       |       |         | www.testingsite.com           |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+

This is a snippet of the query I am using : 这是我正在使用的查询的摘要:

SELECT * FROM `company` C 
LEFT OUTER JOIN owner O USING ( CompanyID ) 
LEFT OUTER JOIN sales S USING ( CompanyID )

You can try something like this in mySQL. 您可以在mySQL中尝试类似的方法。 It will give comma separated websites whenever there are multiple rows with everything same except the website 每当有多行内容相同的网站(网站除外)时,它将为逗号分隔的网站

SELECT *,GROUP_CONCAT(website,',') as website FROM `company` C 
LEFT OUTER JOIN owner O USING ( CompanyID ) 
LEFT OUTER JOIN sales S USING ( CompanyID )
GROUP BY CompanyID

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

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