I’m working on a shop-like system (in PHP & MySQL) that exports invoices to an external system. The proces goes something like this;
- Customer orders a product and an invoice is created
- XML is generated for this invoice and sent to the external server
- External server handles XML and responds with another XML
- System handles response.
For every XML that’s being sent to the external server a record is created in the database containing the corresponding invoicenumber and a status (status is initially SENT, indicating that the XML is sent.) After the system has handled the response the status is either SUCCESS or ERROR. Now, the problem is; at some point I want to fetch a list of invoices that have no record in the request table with status SUCCESS.
EDIT: If the status is ERROR there will be a new request for the same invoice, so there is a possibility that there will be more than one request per invoice.
My order table has columns ID and InvoiceNumber, and the request table has columns ID, InvoiceNumber and Status, so to get the list mentioned I could do something like:
SELECT InvoiceNumber
FROM orders AS a
LEFT JOIN requests AS b
ON a.InvoiceNumber = b.InvoiceNumber
WHERE NOT EXISTS (SELECT ID
FROM requests
WHERE status = "SUCCESS"
AND request.InvoiceNumber = a.InvoiceNumber)
However, a second option would be to create an extra column to the order table (ie. requestSucces) being initially 0 and set to 1 if the system handles a successful response for the corresponding invoice. This would result in a much easier and less expensive query to get the list of invoices that need to be (re)sent (SELECT invoiceNumber FROM orders WHERE requestSuccess = 0), however the field would technically be redundant.
My question is: what would be better; use the easy query at the cost of having a redundant field or use the heavier query and not pollute the database with redundancy. Of course; if any of you know of a better solution without using redundancy that would be even better.
when you make the status field numeric error=0 and succes=1 one you could do a max status grouped by invoicenumber to see which invoices are no