将(*)计数为SQL Query上的JOIN内部

时间:2017-05-03 02:06:11

标签: mysql sql join count

我有一个报告的以下查询都运行正常,但我需要在报告中添加一个变量,该变量根据" manheim_auction_listings&中的记录数总计返回的每条记录的记录数#34;记录。我觉得它需要在一个连接中,但在任何地方我都会" COUNT(*)AS num_of_runs"它似乎使得整个查询只返回一行,其中count是查询中的记录总数,而不是所有带有变量num_of_runs的行,其数量为" manheim_auction_listings"每条CAR记录的记录。

SELECT products.client_id,
    clients.name AS client_name,
    manheim_auction_lanes.lane_number,
    manheim_auction_listings.sequence,
    manheim_auction_listings.gross_sale_price,
    products.asking_price,  products.asking_price_condition, 
    manheim_auctions.auction_date,
    manheim_auctions.auction_number,
    product_purchases.total_spent,
    product_purchases.purchase_price 
FROM manheim_auction_listings
    JOIN cars ON 
    cars.id = manheim_auction_listings.car_id 
    JOIN products ON 
     cars.product_id = products.id 
    JOIN product_purchases ON
     current_product_purchase_id = product_purchases.id
    JOIN manheim_auctions ON 
     manheim_auctions.id = manheim_auction_listings.manheim_auction_id 
    JOIN manheim_auction_lanes ON 
      manheim_auction_lanes.id = manheim_auction_listings.manheim_auction_lane_id 
    JOIN clients ON 
     clients.id = products.client_id 
     AND clients.id LIKE $P{LoggedInUserAttribute_ClientID} 
WHERE 
     manheim_auctions.auction_number = $P{SaleNumber} 
     AND manheim_auctions.`year` = $P{SaleYear} 
ORDER BY manheim_auction_lanes.lane_number DESC,
    manheim_auction_listings.sequence DESC

1 个答案:

答案 0 :(得分:1)

请尝试以下方法......

SELECT products.client_id,
       clients.name AS client_name,
       manheim_auction_lanes.lane_number,
       manheim_auction_listings.sequence,
       manheim_auction_listings.gross_sale_price,
       num_of_runs,
       products.asking_price,  products.asking_price_condition,
       manheim_auctions.auction_date,
       manheim_auctions.auction_number,
       product_purchases.total_spent,
       product_purchases.purchase_price 
FROM ( SELECT manheim_auction_id AS manheim_auction_id,
              COUNT( manheim_auction_id ) AS num_of_runs
       FROM manheim_auction_listings
       GROUP BY manheim_auction_id
     ) AS num_of_runs_finder
JOIN manheim_auction_listings ON manheim_auction_listings.manheim_auction_id = num_of_runs.manheim_auction_id
JOIN cars ON cars.id = manheim_auction_listings.car_id
JOIN products ON cars.product_id = products.id
JOIN product_purchases ON current_product_purchase_id = product_purchases.id
JOIN manheim_auctions ON manheim_auctions.id = manheim_auction_listings.manheim_auction_id
JOIN manheim_auction_lanes ON manheim_auction_lanes.id = manheim_auction_listings.manheim_auction_lane_id
JOIN clients ON clients.id = products.client_id 
            AND clients.id LIKE $P{LoggedInUserAttribute_ClientID}
WHERE manheim_auctions.auction_number = $P{SaleNumber}
  AND manheim_auctions.`year` = $P{SaleYear} 
ORDER BY manheim_auction_lanes.lane_number DESC,
         manheim_auction_listings.sequence DESC

这可以通过将您的其他表格加入到计算与每个manheim_auction_id关联的商家信息数量的表格中来实现,从而有效地将manheim_auction_id的计数附加到manheim_auction_id出现的每一行。

如果根据其他一些标准计算num_of_runs,请相应地告诉我。

如果您有任何问题或意见,请随时发表评论。