连接两个查询以形成结果集

时间:2013-06-27 07:14:05

标签: php mysql

你好我必须加入两个查询才能形成结果集: -

第一

SELECT  
    orderId,
    GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId 
FROM 
    ecart_product 
INNER JOIN ecart_orderdetail 
WHERE 
    ecart_orderdetail.productId = ecart_product.id 
group by orderId

这给了我

orderId catId
167 59, 2
168 2
169 2
170 2
171 2
172 48, 2
173 2
174 2

第二名:

select * from ecart_orders
两个ord​​erId中的

通常如何才能加入?

2 个答案:

答案 0 :(得分:2)

假设orderId表格中ecart_orders为PK,且productId列为{。} 你可以试试这个:

SELECT  
    orderId,
    GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId ,
    ecart_orders.*
FROM 
    ecart_product 
INNER JOIN ecart_orderdetail 
    ON ecart_orderdetail.productId = ecart_product.id 
INNER JOIN ecart_orders
    ON ecart_orders.productId = ecart_product.id    
group by orderId

答案 1 :(得分:0)

同时我也解决了它

  select * from ecart_orders Inner join(
  SELECT  
  orderId,
  GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ')  as catId
  FROM   `ecart_product` INNER JOIN ecart_orderdetail
  WHERE ecart_orderdetail.productId =   ecart_product.id group by orderId) as c 
  on ecart_orders.id=c.orderId