dsour68888 2014-02-18 18:11
浏览 114
已采纳

使用LEFT OUTER JOIN返回一个匹配的行,另外只返回匹配的id

I have a large database that has about 190 columns in 22 tables. 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. 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:

+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| 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 )
  • 写回答

1条回答 默认 最新

  • dtdr84101 2014-02-18 18:23
    关注

    You can try something like this in 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改