三个表连接

时间:2013-02-17 20:07:20

标签: mysql

我想加入三个表productgroupbusinessgroupbusinessunit

这是我的疑问:

SELECT
    `productgroup`.*, businessunit.*

FROM
    `productgroup`
    INNER JOIN `prodgroupbusinessunit` 
        ON (`productgroup`.`prodgroupid` = `prodgroupbusinessunit`.`prodgroupid`)
    INNER JOIN businessunit
        ON (businessunit.`businessunitid` = `prodgroupbusinessunit`.businessunit)

我使用了表的内部联接,上面的查询获取了所有记录但它获得了多个productgroupnames 。 我如何使查询显示productgroupname?

以下是我的表格的结构和数据:

ProductGroup

productgroupid      productgroupname
--------------------------------------------------------------------
1                      tasty                              
2                      creamy
3                      lingering
4                      watering
5                      yummy
6                      swallow

BusinessUnit

businessunitid      businessunitname
--------------------------------------------------------------------
1                      food                              
2                      beverage

ProductGroupBusinessUnit

productgroupbusinessunitid        productgroupid        businessunitid      brand         

---------------------------------------------------------------------------------

1                        1                         1                       brand1
2                        1                         1                       brand1
3                        2                         1                       brand2
4                        3                         1                       brand3
5                        4                         1                       brand4
6                        1                         2                       brand5
7                        2                         2                       brand6
8                        5                         2                       brand7
9                        6                         1                       brand8
10                       6                         2                       brand9
---------------------------------------------------------------------------------

0 个答案:

没有答案