Had this query licked until the specs changed.. Now I'm a bit stuck.
In words:
I need to find transactions in a given range that have a certain payment method (VHR RED).
For those, I need to get the transaction data and any other items they might have.
Best I could come up with was to get the transaction ID's that fell in the range & had a 'VHR RED' in them.
Then use ^ inside an IN() with a second query.
The query to get the ids list:
Code:
SELECT GROUP_CONCAT(t.id) as ids
FROM transactions t
LEFT JOIN transactions_items ti
ON ti.transaction_id = t.id
WHERE d.date_added
BETWEEN ".$values['start_date']."
AND "'.$values['end_date'].'"
AND ti.pay_method = "VHR RED"
ORDER BY t.date_added DESC
And the query to get the 'real' required data
Code:
SELECT t.dps as "DPS"
, ti.amount as "Inclusive Amount"
, DATE_FORMAT(t.date_added, "%d/%c/%Y %T") as "Transaction Date"
, DATE_FORMAT(v.expiry_date, "%d/%c/%Y %T") as "Expiry Date"
, v.hash as "Voucher Code"
, ti.pay_method as "Payment Type"
FROM transactions t
LEFT JOIN transaction_items ti
ON ti.transaction_id = t.id
LEFT JOIN vouchers v
ON v.id = ti.voucher_id
WHERE t.id
IN ('.$rows['ids'].')
ORDER BY d.date_added DESC
Left a little bit of application code in there for dates and the id csv.
It does work fine but from what I've read about IN() it seems to be avoided if possible.
I figure the first query should be a sub select in a join but can't quite get there...
Thanks for any help.