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 : Sélectionner tout - Visualiser dans une fenêtre à part
$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 php : Sélectionner tout - Visualiser dans une fenêtre à part
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']);?>&nbspFCFA</span></td>
</tr>
<td><?php endwhile; ?></td>
</tbody>
</table>
</body>
</html>
*
*