PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Create the 'doleance' table CREATE TABLE doleance ( id SERIAL PRIMARY KEY, interet CHAR(1) NOT NULL CHECK (interet IN ('a', 'b', 'c', 'd', 'e', 'f')), etat VARCHAR(10) NOT NULL CHECK (etat IN ('traite', 'non traite')), year INT NOT NULL, description TEXT, date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Insert sample data into the 'doleance' table INSERT INTO doleance (interet, etat, year, description) VALUES ('a', 'traite', 2024, 'Description A1'), ('a', 'non traite', 2024, 'Description A2'), ('b', 'traite', 2024, 'Description B1'), ('b', 'non traite', 2023, 'Description B2'), ('c', 'traite', 2023, 'Description C1'), ('c', 'non traite', 2023, 'Description C2'), ('d', 'traite', 2022, 'Description D1'), ('d', 'non traite', 2022, 'Description D2'), ('e', 'traite', 2021, 'Description E1'), ('e', 'non traite', 2021, 'Description E2'), ('f', 'traite', 2020, 'Description F1'), ('f', 'non traite', 2020, 'Description F2'), ('a', 'traite', 2023, 'Description A3'), ('a', 'non traite', 2022, 'Description A4'), ('b', 'traite', 2021, 'Description B3'), ('c', 'non traite', 2020, 'Description C3'), ('d', 'traite', 2024, 'Description D3'), ('e', 'non traite', 2024, 'Description E3'), ('f', 'traite', 2024, 'Description F3'), ('a', 'non traite', 2021, 'Description A5'); -- Insert additional sample data into the 'doleance' table INSERT INTO doleance (interet, etat, year, description) VALUES -- Additional records for 'a' ('a', 'traite', 2023, 'Description A6'), ('a', 'non traite', 2023, 'Description A7'), ('a', 'traite', 2022, 'Description A8'), ('a', 'non traite', 2022, 'Description A9'), ('a', 'traite', 2021, 'Description A10'), ('a', 'non traite', 2021, 'Description A11'), -- Additional records for 'b' ('b', 'traite', 2024, 'Description B4'), ('b', 'non traite', 2024, 'Description B5'), ('b', 'traite', 2022, 'Description B6'), ('b', 'non traite', 2021, 'Description B7'), -- Additional records for 'c' ('c', 'traite', 2024, 'Description C4'), ('c', 'non traite', 2024, 'Description C5'), ('c', 'traite', 2022, 'Description C6'), ('c', 'non traite', 2022, 'Description C7'), -- Additional records for 'd' ('d', 'traite', 2023, 'Description D4'), ('d', 'non traite', 2023, 'Description D5'), ('d', 'traite', 2021, 'Description D6'), ('d', 'non traite', 2021, 'Description D7'), -- Additional records for 'e' ('e', 'traite', 2023, 'Description E4'), ('e', 'non traite', 2023, 'Description E5'), ('e', 'traite', 2022, 'Description E6'), ('e', 'non traite', 2022, 'Description E7'), -- Additional records for 'f' ('f', 'traite', 2023, 'Description F4'), ('f', 'non traite', 2023, 'Description F5'), ('f', 'traite', 2021, 'Description F6'), ('f', 'non traite', 2021, 'Description F7'), -- Mixed records across years ('a', 'traite', 2020, 'Description A12'), ('b', 'non traite', 2020, 'Description B8'), ('c', 'traite', 2019, 'Description C8'), ('d', 'non traite', 2019, 'Description D8'), ('e', 'traite', 2018, 'Description E8'), ('f', 'non traite', 2018, 'Description F8'), ('a', 'traite', 2017, 'Description A13'), ('b', 'non traite', 2017, 'Description B9'), ('c', 'traite', 2016, 'Description C9'), ('d', 'non traite', 2016, 'Description D9'); -- Select the inserted data to verify SELECT * FROM doleance;
Copy Clear
Copy Format Clear
<?php use Carbon\Carbon; $now = Carbon::now()->format('d/m/Y'); printf("Today is %s\nCurrent PHP version: %s \n\n", $now, phpversion()); $query = "SELECT VERSION() as version;"; // get DB version using PDO $stmt = $pdo->prepare($query); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); printf('DB version (PDO): %s ' . PHP_EOL, $row['version']); // Run query using mysqli $result = $mysqli->query($query); $version = $result->fetch_object(); printf('DB version (mysqli): %s ' . PHP_EOL, $version->version); // Select using Laravel $version = $db::select($query); printf('DB version (Laravel Query Builder): %s ' . PHP_EOL, $version[0]->version); //---------------------------------------------- // get DB version using PDO function getDoleanceCounts1($pdo){ $query = "SELECT * FROM doleance;"; $stmt = $pdo->prepare($query); $stmt->execute(); $row = $stmt->fetchall(PDO::FETCH_ASSOC); return $row; } //print_r(getDoleanceCounts1($pdo)); function getDoleanceCounts($pdo, $byYear = false) { // Base SQL query $sql = " SELECT " . ($byYear ? "year AS yrar," : "0 AS yrar,") . " interet AS inte, SUM(CASE WHEN etat = 'traite' THEN 1 ELSE 0 END) AS trait, SUM(CASE WHEN etat = 'non traite' THEN 1 ELSE 0 END) AS non_trait, COUNT(*) AS total FROM doleance GROUP BY interet" . ($byYear ? ", year" : ""); $stmt = $pdo->prepare($sql); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); // Group by year or globally $finalResults = []; $globalCounts = []; foreach ($results as $row) { $year = $byYear ? $row['yrar'] : 0; $interet = $row['inte']; // Initialize the year array if it doesn't exist if (!isset($finalResults[$year])) { $finalResults[$year] = []; } // Add the row to the final results $finalResults[$year][] = $row; // Aggregate global counts if (!isset($globalCounts[$interet])) { $globalCounts[$interet] = ['trait' => 0, 'non_trait' => 0, 'total' => 0]; } $globalCounts[$interet]['trait'] += $row['trait']; $globalCounts[$interet]['non_trait'] += $row['non_trait']; $globalCounts[$interet]['total'] += $row['total']; } // Add the "all" counts foreach ($finalResults as $year => $rows) { $allCounts = ['yrar' => $year, 'inte' => 'all', 'trait' => 0, 'non_trait' => 0, 'total' => 0]; foreach ($rows as $row) { $allCounts['trait'] += $row['trait']; $allCounts['non_trait'] += $row['non_trait']; $allCounts['total'] += $row['total']; } $finalResults[$year][] = $allCounts; } // If not by year, return only the global counts if (!$byYear) { $finalResults[0] = []; foreach ($globalCounts as $interet => $counts) { $finalResults[0][] = [ 'yrar' => 0, 'inte' => $interet, 'trait' => $counts['trait'], 'non_trait' => $counts['non_trait'], 'total' => $counts['total'], ]; } // Add the global "all" counts $finalResults[0][] = [ 'yrar' => 0, 'inte' => 'all', 'trait' => array_sum(array_column($globalCounts, 'trait')), 'non_trait' => array_sum(array_column($globalCounts, 'non_trait')), 'total' => array_sum(array_column($globalCounts, 'total')), ]; } return json_encode(array_values($finalResults)); } // Global count without year details $globalCounts = getDoleanceCounts($pdo, false); echo $globalCounts; // Count detailed by year $yearlyCounts = getDoleanceCounts($pdo, true); echo $yearlyCounts;
Show:  
Copy Clear