Obtenir le total de deux ou plusieurs tables
Bonjour,
J’ai les deux tables suivantes :
Table1
Id |
noms |
montant |
post |
date |
1 |
cedric |
500 |
1w |
2021-07-25 |
2 |
Roland |
1200 |
1w |
2021-07-25 |
3 |
Alain |
800 |
1w |
2021-07-26 |
4 |
Loïs |
700 |
1w |
2021-07-26 |
Table2
id |
noms |
montant |
post |
date |
1 |
Sylvie |
400 |
1w |
2021-07-25 |
2 |
Jules |
900 |
1w |
2021-07-25 |
3 |
marc |
300 |
1w |
2021-07-26 |
4 |
Laure |
1400 |
1w |
2021-07-26 |
Je veux faire le total des sommes des deux tables.
Soit somme1(table1) = 3200
Somme2(table2) = 3000
Total = 3200+3000 = 6200
Dans les deux tables la colonne ‘post’ est pour la jointure.
En voulant obtenir le total= 6200 , j’ai fait la requête suivante :
Code:
$sql = "SELECT c.montant,t1.montant,SUM(t1.montant+t2.montant) AS total FROM table1 t1 INNER JOIN table2 t2 ON t2.post = t1.post ";
Seulement ma requête ne fonctionne bien que dans le cas suivant où j’obtiens total = 900 :
Table1
Id |
noms |
montant |
post |
date |
1 |
cedric |
500 |
1w |
2021-07-25 |
2 |
Roland |
1200 |
NULL |
2021-07-25 |
3 |
Alain |
800 |
NULL |
2021-07-26 |
4 |
Loïs |
700 |
NULL |
2021-07-26 |
Table2
id |
noms |
montant |
post |
date |
1 |
Sylvie |
400 |
1w |
2021-07-25 |
2 |
Jules |
900 |
NULL |
2021-07-25 |
3 |
marc |
300 |
NULL |
2021-07-26 |
4 |
Laure |
1400 |
NULL |
2021-07-26 |
J’ai lu les faq et tutoriels en vain. Je n’y arrive pas.
Merci d’avance pour votre aide.
Mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?php
$host = 'localhost';
$dbname = 'bdd';
$username = 'Y';
$password = 'X';
$dsn = ("mysql:host=$host;dbname=$dbname");
// récupérer tous les utilisateurs
$sql = "SELECT t1.montant,t2.montant,SUM(t1.montant+t2.montant) AS total FROM table1 t1 INNER JOIN table2 t2 ON t2.post = t1.post ";
try{
$pdo = new PDO($dsn, $username, $password);
$stmt = $pdo->query($sql);
if($stmt === false){
die("Erreur");
}
}catch (PDOException $e)
{
echo $e->getMessage();}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Le total des deux tables</h1>
<table width="80%" border="1" cellspacing="0" bordercolor="#FFFFFF">
<thead>
<tr>
<td><span style="position: absolute; bottom: 200px; left: 10px; width: 242px; heigth: 70px;">
TOTAL =
</span></td>
</tr>
</thead>
<tbody>
<?php while($row = $stmt->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><span style="position: absolute; bottom: 200px; left: 220px; width: 242px; heigth: 70px;"><?php echo htmlspecialchars($row['total']);?> FCFA</span></td>
</tr>
<td><?php endwhile; ?></td>
</tbody>
</table>
</body>
</html>
*
* |
Obtenir le total des sommes de deux colonnes de deux tables en fonction de la date d'enregistrement
Bonjour ,
Je veux à partir de deux tables suivantes, obtenir le total de deux colonnes en fonction d'une date d'enregistrement. Comme cet exemple :
Table1
Désignation |
prix. |
Date_achat |
Stylo. |
500. |
02-10-2021 |
Crayon. |
300 |
02-10-2021 |
Cahier. |
1000 |
05-10-2021 |
Règle. |
700 |
05-10-2021 |
Table2:
Désignation |
prix. |
Date_achat |
Gomme. |
400. |
02-10-2021 |
Compas. |
600 |
02-10-2021 |
Ma requête est:
Code:
Sreq = " SELECT (SELECT ALL SUM(prix) FROM table1 WHERE date_achat LIKE ?) + (SELECT ALL SUM(prix) FROM table2 WHERE date_achat LIKE ?) AS total"
Lorsque je soumets la date_achat =02-10-2021, j'ai le total = 1800, ce total est juste.
Par contre lorsque je soumets la date_achat =05-10-2021, le total ne s'effectue pas alors que je m'attends à un total=1700.
Je suis bloqué , merci d'avance pour votre aide.