I am writing reports for my school's lunch order program, I have made a query that gives me the results I need but is very very slow. I was thinking of using correlated sub queries to speed up (suggestion from another similar thread) but can't get past syntax errors and am not even sure if it will accomplish what I want.
The two tables involved are Ordered_Items and Ordered_Options the Ordered Items table would generally have 3 - 4 related records in the Ordered Options table which represent the options the students choose when ordering lunch.
A typical record would look like this
Ordered_Items.Record_Number = 1
Ordered_Items.Name = Pizza
Ordered_Options.Ordered_Items_rn = 1
Ordered_Options.Value = Jane
Ordered_Options.rn = 43
Ordered_Options.Ordered_Items_rn = 1
Ordered_Options.Value = Doe
Ordered_Options.rn = 44
Ordered_Options.Ordered_Items_rn = 1
Ordered_Options.Value = Pepperoni
Ordered_Options.rn = 45
I would like them all to show up as a single record for reporting purposes so the output from the query would look like: Pizza Jane Doe Pepperoni. The Ordered_Options.rn are consistent meaning that the first name is always = 44 and so on. I have put my code below.
SELECT
OI.Name AS ItemName,
opt1.`Value` AS StudentFirst,
opt2.`Value` AS StudentLast,
opt3.`Value` AS Grade,
opt4.`Value` AS Milk
FROM
((((
Ordered_Items OI
LEFT JOIN Ordered_Options opt1 ON ((
(OI.record_number = opt1.Ordered_Items_rn)
AND(opt1.Options_rn = 43))))
LEFT JOIN Ordered_Options opt2 ON((
(OI.record_number = opt2.Ordered_Items_rn)
AND(opt2.Options_rn = 44))))
LEFT JOIN Ordered_Options opt3 ON ((
(OI.record_number = opt3.Ordered_Items_rn)
AND(opt3.Options_rn = 46))))
LEFT JOIN Ordered_Options opt4 ON ((
(OI.record_number = opt4.Ordered_Items_rn)
AND(opt4.Options_rn = 55))))