Hello. I want to populate column in my table with values that are results of a arithmetic operation. Operation take operands from another tables
Here is the details. There is my first table:
CREATE TABLE `farrowing` (
`id` int(11) NOT NULL,
`user` varchar(11) NOT NULL,
`sowNumber` varchar(20) NOT NULL,
`cycleNumber` int(11) NOT NULL,
`penNumber` int(11) NOT NULL,
`sectionNumber` int(11) NOT NULL,
`pigletBornNumber` int(11) NOT NULL,
`livePigletBorn` int(11) NOT NULL,
`farrowingDate` date NOT NULL,
`currentInFarrHouse` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_ge
INSERT INTO `farrowing` (`id`, `user`, `sowNumber`, `cycleNumber`, `penNumber`, `sectionNumber`, `pigletBornNumber`, `livePigletBorn`, `farrowingDate`, `currentInFarrHouse`) VALUES
(21, '1', '0001', 1, 1, 0, 12, 10, '2023-12-22', 1),
(22, '1', '0002', 1, 2, 0, 14, 13, '2023-12-23', 1),
(23, '1', '0003', 1, 3, 0, 17, 13, '2023-12-08', 1);
I want to show current number of piglets in the farrowing house. When I change data in my farrowing table I want the number of piglets to change accordingly. There is the other three tables:
CREATE TABLE `pigletlose` (
`id` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateOfLose` date NOT NULL,
`cycle` int(11) NOT NULL,
`penNumber` int(11) NOT NULL,
`numberOfPiglets` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `pigletchange` (
`id` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`cycle` int(11) NOT NULL,
`penFrom` int(11) DEFAULT NULL,
`penTo` int(11) DEFAULT NULL,
`pigletNumber` int(11) NOT NULL,
`dateOfChange` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `weaningindi` (
`id` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateOfWeaning` date NOT NULL,
`cycle` int(11) NOT NULL,
`penFrom` int(11) NOT NULL,
`numberOfPiglets` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;`
My arithmetic is: (number of piglets in pen in farrowing table) - (number of piglets lost in pigletlose table)-( number of piglets that are transferred to another pens in pigletchange table)+( piglets that came into the pen in pigletchange table)- ( weaned pigs from that pen in weanindi table)
I tried to fetch the data from farrowing table and then use that values for arithmethic, but result of the subqueries is null
$sql = "
SELECT
sowNumber,
cycleNumber,
penNumber,
pigletBornNumber,
livePigletBorn,
farrowingDate,
(
SELECT IFNULL(livePigletBorn, 0) FROM farrowing WHERE penNumber = farrowing.penNumber LIMIT 1
) AS livePigletBorn,
(
SELECT IFNULL(numberOfPiglets, 0) FROM pigletlose WHERE penNumber = farrowing.penNumber LIMIT 1
) AS numberOfPigletsLost,
(
SELECT IFNULL(pigletNumber, 0) FROM pigletchange WHERE penTo = farrowing.penNumber LIMIT 1
) AS pigletsChangedIn,
(
SELECT IFNULL(pigletNumber, 0) FROM pigletchange WHERE penFrom = farrowing.penNumber LIMIT 1
) AS pigletsChangedOut,
(
SELECT IFNULL(numberOfPiglets, 0) FROM weaningindi WHERE penNumber = farrowing.penNumber LIMIT 1
) AS numberOfPigletsWeaned
FROM
farrowing
WHERE
user = '$userId' AND currentInFarrHouse = 1";
$result = $mysqli->query($sql);
if ($result) {
echo '<table id="data_table" class="table table-striped">';
echo '<tr>
<th>Korisnik</th>
<th>Broj krmače</th>
<th>Broj ciklusa</th>
<th>Broj boksa</th>
<th>Broj prasadi pri rođenju</th>
<th>Broj žive prasadi pri rođenju</th>
<th>Datum prašenja</th>
<th>Žive prasadi</th>
<th>Broj prasadi izgubljenih</th>
<th>Broj prasadi promenjenih (unutra)</th>
<th>Broj prasadi promenjenih (spolja)</th>
<th>Broj prasadi odviknutih</th>
</tr>';
while ($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $userId . '</td>';
echo '<td>' . $row['sowNumber'] . '</td>';
echo '<td>' . $row['cycleNumber'] . '</td>';
echo '<td>' . $row['penNumber'] . '</td>';
echo '<td>' . $row['pigletBornNumber'] . '</td>';
echo '<td>' . $row['livePigletBorn'] . '</td>';
echo '<td>' . $row['farrowingDate'] . '</td>';
echo '<td>' . $row['livePigletBorn'] . '</td>';
echo '<td>' . $row['numberOfPigletsLost'] . '</td>';
echo '<td>' . $row['pigletsChangedIn'] . '</td>';
echo '<td>' . $row['pigletsChangedOut'] . '</td>';
echo '<td>' . $row['numberOfPigletsWeaned'] . '</td>';
echo '</tr>';
}
echo '</table>';
$result->close();
} else {
echo "Error executing the SQL query: " . $mysqli->error;
}
I also tried with some foreach loop. Once again, need to populate a column that contains number of piglets in some pen. For that I have to take data about number of piglets in another tables and do the math. I hope it can be done simply, but I have no clue. Appreciate any help.