简体   繁体   中英

How to use MySQL SELECT results as variables and then use the variables in another SELECT/UNION statement?

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.

If you want to make this using multiple queries you can go with do while. I think we can do this by using single query like below.

SELECT items.name, data.clock, data.value
FROM data
LEFT JOIN items ON data.itemid = items.itemid
LEFT JOIN hosts ON hosts.hosstid = items.hostid
WHERE hosts.host LIKE 'keyword';
GROUP BY data.value
ORDER BY items.name, data.clock ASC
LIMIT 15;

the best way is to recover the information from the 1st query and then do a while:

$x=0;
while($row=$result->mysqli_fetch_array()){ //$result is the variable where you have the result of the 1st query execution
    //execute second query with $row[0], $row[1]... as variables, get the results in the variable $result2.
    //If you need groups of 15 elements do something like: 
    $y=$x*15;
    $query2="SELECT .... LIMIT $y, 15";
    $result2=$mysqli->query($query2);
    while($row2=$result2->mysqli_fetch_array()){
        //do something with the results of the 2nd query, stored in $row2[0], $row2[1], etc
    }
    $x++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM