2014-02-11 149 views
0

我得到了以下的mysql结构:SQL |获得产品和产品变

`product` 
id | name  | quantity | unit 
10 | Color bath | 12   | L 
20 | Color house | 15   | L 

`product_variant` 
id | masterid | name  | quantity | unit 
100 | 10   | Color bath | 10   | L 
200 | 10   | Color bath | 20   | L 
300 | 20   | Color house | 20   | L 

什么我们的查询应该返回:

给我的所有产品,并给我从这个产品的所有productvariants并返回每个产品/ product_variant一条线:

product.id as masterid, product.id/product_variant.id, product.name, product.quantity/product_variant.quantity, product.unit/product_variant.unit 
10, 10, Color bath, 12, L 
10, 100, Color bath, 10, L 
10, 200, Color bath, 20, L 
20, 20, Color house, 15, L 
20, 300, Color house, 20, L 

我们试过使用UNION SELECT,但这似乎不是正确的解决方案。 10.000行(使用索引)最多需要2秒。 有没有其他的机会得到我们需要什么,而不使用UNION?

回答

0

我不认为你正在尝试做没有UNION可能是因为你需要连接两个表productproduct_variant,当你加入他们的行列,你会得到(如你的例子),两行product.id = 10没有更多的不少,而你期待3.