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

时间:2014-02-18 18:11:11

标签: php mysql sql join

我有一个大型数据库,在22个表中有大约190列。有几个表允许多个条目进入数据库,所有值都由外键引用。当我使用LEFT OUTER JOIN时如果在与特定ID匹配的单个列中有多个条目,则它会创建一个包含所有信息的新行,如前所述仅更改表字段。例如:

+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| 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           |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+

你看到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           |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+

这是我正在使用的查询的片段:

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

1 个答案:

答案 0 :(得分:1)

你可以在mySQL中尝试这样的东西。只要有多行,除了网站

之外,它都会给逗号分隔的网站
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