My title doesn’t quite sum up what I need correctly but hopefully it will be clearer as I explain what I’m trying to do. I’m trying to create a PHP based webpage that takes an entered keyword and uses it to output a selection of data to an excel spreadsheet.
My first statement uses the keyword to find what ‘itemid’ it refers to which is listed in its own table (the separate tables for keys and ids is not ideal but i cannot change this):
SELECT items.itemid
FROM items
LEFT JOIN hosts ON hosts.hostid = items.hostid
WHERE hosts.host LIKE 'keyword';
This returns a selection of numbers that could return 0 results or 10+ results.
Now I need to return a selection of results based on these results.
For example:
SELECT items.name, data.clock, data.value
FROM data
LEFT JOIN items ON data.itemid = items.itemid
WHERE item.itemid = '<First result from previous statement>'
GROUP BY data.value
ORDER BY items.name, data.clock ASC
LIMIT 15
UNION ALL
SELECT items.name, data.clock, data.value
FROM data
LEFT JOIN items ON data.itemid = items.itemid
WHERE item.itemid = '<Second result from previous statement>'
GROUP BY data.value
ORDER BY items.name, data.clock ASC
LIMIT 15
...
So on for however many results there were
...
SELECT items.name, data.clock, data.value
FROM data
LEFT JOIN items ON data.itemid = items.itemid
INTO OUTFILE '/file/location.csv' FIELDS ..ect
WHERE item.itemid = '<x result from previous statement>'
GROUP BY data.value
ORDER BY items.name, data.clock ASC
LIMIT 15
I would use WHERE IN instead of UNION ALL but I need to limit it to 15 results per value as some can have up to 100,000+ rows.
I was thinking it may be possible to enter the results of the first statement into an array and then use that array as variables in the second statement, but as the amount of variables could be different every time I’m not sure how i could write the code in such a way that it repeats the code the necessary amount of times and puts the INTO OUTFILE in the correct place.
I had some trouble getting what i need into words, so if anything is unclear please let me know and I will provide any needed information as soon as I can.
Thanks for any help in advance.
the best way is to recover the information from the 1st query and then do a while: