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

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大