简体   繁体   中英

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. 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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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