I’m new to MySQL, and I’m running this query,
SELECT item_id,amount FROM db.invoice_line WHERE item_id = 'xxx'
OR item_id = 'yyy'
...
AND invoice_id IN
(SELECT id_invoices FROM db.invoices
WHERE customer = 'zzzz'
AND transaction_date > DATE_SUB(NOW(), INTERVAL 6 MONTH)
AND sales_rep = 'aaa') ORDER BY item_id;
That is, select some columns from a table where a foreign key is found in another table.
The issue is that I would like to also have, in the results, the customer name. However, the customer name is not found in the invoice line table, it is found in the invoice table.
While I could naively create a duplicate index upon table creation and inserts, I was wondering if there was a SQL way to select the proper row from the invoice table and have it in the result sets.
Is the performance better if I just duplicate data?
Thanks,
Dane
How about something like this?