简体   繁体   English

如何将MySQL SELECT结果用作变量,然后在另一个SELECT / UNION语句中使用变量?

[英]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. 我正在尝试创建一个基于PHP的网页,该网页采用输入的关键字并将其用于将选择的数据输出到excel电子表格。

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): 我的第一条语句使用关键字查找在其自己的表中列出的“ itemid”(单独的键和ID表并不理想,但我无法更改):

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. 这将返回一组可能返回0个结果或10个以上结果的数字。 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. 我将使用WHERE IN代替UNION ALL,但我需要将其限制为每个值15个结果,因为有些值最多可以包含100,000+行。

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. 我当时在想可能将第一个语句的结果输​​入到数组中,然后将该数组用作第二个语句中的变量,但是由于每次我不确定如何写时变量的数量都可能不同以某种方式重复执行代码,使代码重复必要的次数,然后将INTO OUTFILE放在正确的位置。

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. 如果要使用多个查询来进行此操作,可以同时执行do。 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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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