Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:51:06+00:00 2026-06-14T04:51:06+00:00

My title doesn’t quite sum up what I need correctly but hopefully it will

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:51:07+00:00Added an answer on June 14, 2026 at 4:51 am

    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++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, sorry if my title doesn't quite explain the situation, I had problems thinking
Title doesn't actually reflect what I'm trying to explain. Is it possible to achieve
I know the title doesn't capture what I'm trying to ask but here is
The title doesn't really explain what i want but basically. I have my application
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
The title doesn't quite capture what I mean, and this may be a duplicate.
The question from title doesn't need more details.
I'm sorry if this title doesn't describe the problem properly but I wasn't sure
I'm not too great with jquery and maybe this title doesn't explain too well
The title doesn't describe this very well, but assuming the following simplified table in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.