Okay, so a friend told me that I can't do it w/ one query but that I should be able to do it w/ a nested query. We are running MySQL 4.1.7 on this server, so I think I should be able to pull this off. I thought I had it for a second, but I can't get it to return the right counts. For example, if I run just part of it:
SELECT cart_ordr_hdr. * ,
COUNT( cart_ordr_itms.itm_id ) AS ordr_itm_count,
SUM( cart_ordr_itms.itm_prd_price * cart_ordr_itms.itm_qty_requested ) AS ordr_total
FROM cart_ordr_hdr
LEFT JOIN cart_ordr_itms ON cart_ordr_hdr.ordr_id = cart_ordr_itms.ordr_id
WHERE 1
GROUP BY cart_ordr_hdr.ordr_id
ORDER BY ordr_received_on DESC
LIMIT 0 , 50
This returns the item_counts of 3 and 1 for the two orders that are in the database. And then I can run this query:
SELECT COUNT( itm_prd_id ) AS backorder_count
FROM cart_ordr_itms AS cart_bkordr_itms1
LEFT JOIN cart_ordr_hdr ON cart_ordr_hdr.ordr_id = cart_bkordr_itms1.ordr_id
WHERE cart_ordr_hdr.ordr_id = cart_bkordr_itms1.ordr_id AND cart_bkordr_itms1.itm_prd_active_on >0
GROUP BY cart_ordr_hdr.ordr_id
ORDER BY ordr_received_on DESC LIMIT 0 , 30
And I get the correct backorder_counts of 2 and 1 for the two orders. Those are the correct counts from each query. But when I combine them into one, nested query like this:
SELECT cart_ordr_hdr.*,
COUNT( cart_ordr_itms.itm_id ) AS ordr_itm_count,
SUM( cart_ordr_itms.itm_prd_price * cart_ordr_itms.itm_qty_requested ) AS ordr_total,
cart_bkordr_itms.backorder_count
FROM cart_ordr_hdr
LEFT JOIN cart_ordr_itms ON cart_ordr_hdr.ordr_id = cart_ordr_itms.ordr_id
INNER JOIN (
SELECT COUNT(itm_prd_id) AS backorder_count
FROM cart_ordr_itms AS cart_bkordr_itms1
LEFT JOIN cart_ordr_hdr ON cart_ordr_hdr.ordr_id = cart_bkordr_itms1.ordr_id
WHERE cart_ordr_hdr.ordr_id = cart_bkordr_itms1.ordr_id AND cart_bkordr_itms1.itm_prd_active_on >0
GROUP BY cart_ordr_hdr.ordr_id
ORDER BY ordr_received_on DESC
) AS cart_bkordr_itms
WHERE 1
GROUP BY cart_ordr_hdr.ordr_id
ORDER BY ordr_received_on DESC
LIMIT 0 , 50
... it returns item counts of 6 and 2, and then backorder_counts of 2 and 2. Any ideas??
Thanks,
Shaun