简体   繁体   中英

How can I retrieve MySQL data into a PHP array and then use that array to run another query?

I have a MySQL database in which there is a table that serves as a master list of representative names ids and his/her respective manager, a table that serves as a list of managers, and four additional tables which serve as time intervals in which daily sales data is recorded (calls taken, time taken after each call, revenue, envelopes, pens and other). I am attempting to gather all the statistics for each representative under a single given manager, four times in a day. My thinking is that I should first gather the names and ids under a given manager, and then use that array(?) to run a query through the various tour intervals to gather the sales statistics. What is the best way to do this?

<?php
include 'db_connect.php';
// Get last name of manager
$query = "SELECT * FROM managers WHERE id = '" . $_SESSION['manager_name'] ."';";

$result = $mysqli -> query($query);

$row = $result -> fetch_array(MYSQLI_NUM);
$manager_first = $row[1];
$manager_last = $row[2];

// Get team members
$rep_query = "SELECT * FROM rep_master WHERE leader = '" . $manager_last . "';";

$rep_list = array();
$rep_result = $mysqli -> query($rep_query);

if ($rep_result) {
    while ($rep_row = $rep_result -> fetch_row()) {
        array_push($rep_list, $rep_row[1] . " " . $rep_row[2]);
    }
};

sort($rep_list);

print_r($rep_list);
// Stat query (using my ID as a testing point)
$stat_query = "SELECT id, total_calls, acw, revenue, envelopes, pens, other FROM tour_1 WHERE id='T441241';";

// Get row data
$stat_row = $stat_result -> fetch_array(MYSQLI_NUM);
echo '<br /><br />';
print_r($stat_row);

?>

Use IN and implode:

print_r($rep_list);
// Stat query (using my ID as a testing point)
$stat_query = "SELECT id, total_calls, acw, revenue, envelopes, pens, other 
               FROM tour_1 WHERE id IN(".implode(",",$rep_list).")";

Assuming your tour tables all have the same structure, you could use UNIONs on the four shift tables and two LEFT JOINs to do this in a single query. I've added a column to show which tour the info came from. Here, it's not clear how you want to group and filter the statistics, so you may get multiple rows with the same leader and rep. You can add GROUP BY modifiers within the combinedtours nested query and WHERE filters or ORDER BY modifiers at the appropriate locations to optimise the query and group, filter and order the records accordingly.

SELECT `managers`.*, CONCAT(`rep_master`.`first_name`, ' ', `rep_master`.`last_name`), combinedtours.* FROM `managers` 

RIGHT JOIN 
`rep_master` ON `rep_master`.`leader` = `managers`.`last_name`

LEFT JOIN
(SELECT id, 1 as tour, total_calls, acw, revenue, envelopes, pens, other FROM tour_1
UNION
SELECT id, 2 as tour, total_calls, acw, revenue, envelopes, pens, other FROM tour_2
UNION
SELECT id, 3 as tour, total_calls, acw, revenue, envelopes, pens, other FROM tour_3
UNION
SELECT id, 4 as tour, total_calls, acw, revenue, envelopes, pens, other FROM tour_4
) combinedtours
ON combinedtours.`id` = `rep_master`.`rep_id`

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