Précédent   Forum des professionnels en informatique > Bases de données > MySQL > Débuter
Débuter Forum d'entraide pour débuter avec MySQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 16/11/2011, 14h42   #1
Nouveau Membre du Club
 
Homme
Inscription : février 2008
Messages : 54
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Algérie

Informations forums :
Inscription : février 2008
Messages : 54
Points : 32
Points : 32
Par défaut requête qui calcules les sommes

bonsoir tout le monde ... dans cette requête je veux affiche le total des consommation , pour les plats il affiche le bon résultat mais pour la boisson ou les Gâteau il m'affiche le résultat * nombre de linge dans la table contenirplat) merci d'avance pour l'aide svp



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
SELECT  DISTINCT clienthotel.nom, clienthotel.prenom,clienthotel.telephone, clienthotel.numPieceIdentification,clienthotel.typePieceIdentification,
       reserverchambre.dateFin-reserverchambre.dateFin AS NombreJours ,
 
 sum(contenirplat.nombrePlat*plat.prix)AS Plats, 
 sum(contenirgateau.nombreGateau*gateaupatisserie.prix) AS Gateaux , 
 sum(contenirboisson.quantite*boisson.prix)AS Boisson
 
FROM clienthotel,reserverchambre,roomservice,commanderestaurant,contenirplat,contenirboisson,contenirgateau,plat,boisson,gateaupatisserie
 
WHERE clienthotel.id=reserverchambre.idClientHotel
 
AND roomservice.idReserverchambre=reserverchambre.id
 
AND roomservice.idCommanderestaurant=commanderestaurant.id
 
AND commanderestaurant.id=contenirplat.idCommandeRestaurant
 
AND commanderestaurant.id=contenirboisson.idCommandeRestaurant
 
AND commanderestaurant.id=contenirgateau.idCommandeRestaurant
 
AND contenirplat.idPlat=plat.id
 
AND contenirboisson.idBoisson=boisson.id
 
AND contenirgateau.idGateauPatisserie=gateaupatisserie.id
 
AND reserverchambre.id=16
GROUP BY reserverchambre.id
bingo06260 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/11/2011, 15h35   #2
Expert Confirmé
 
Avatar de Maljuna Kris
 
Homme Avcxjo MoKo
Retraité
Inscription : novembre 2005
Messages : 2 530
Détails du profil
Informations personnelles :
Nom : Homme Avcxjo MoKo
Âge : 60

Informations professionnelles :
Activité : Retraité
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : novembre 2005
Messages : 2 530
Points : 3 523
Points : 3 523
Saluton,
Plusieurs choses à propos de ta requête, d'abord les jointures s'écrivent depuis 1992 avec les syntaxes normalisées [INNER] [OUTER LEFT | RIGHT] JOIN ON...condition
D'autre part, à quoi bon faire un GROUP BY reserverchambre.id puisque que le filtre WHERE limite la requête aux lignes reserverchambre.id=16 ?

Pour le reste, les jointures multi-tables retournent autant de lignes qu'il y a de correspondances dans la jointure de plus grande profondeur, sorte de produit cartésien restreint, donc il peut y avoir des lignes d'une même table qui y participent plusieurs fois.
__________________
Kie lumo eksistas ankaŭ ombro troviĝas. L.L. Zamenhof
articles : Comment émuler un tableau croisé [quasi] dynamique
et : Une énigme mathématique résolue avec MySQL
recommande l'utilisation de PDO (PHP5 Data Objects)
Maljuna Kris est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/11/2011, 22h53   #3
Nouveau Membre du Club
 
Homme
Inscription : février 2008
Messages : 54
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Algérie

Informations forums :
Inscription : février 2008
Messages : 54
Points : 32
Points : 32
voila j'ai cet solution , mais le problème je veux affiche chaque résultat dans une colonne au lieux de tout dans une colonne
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
 SELECT sum(contenirplat.nombrePlat*plat.prix)AS Plats
 
FROM clienthotel,reserverchambre,roomservice,commanderestaurant,contenirplat,plat
 
WHERE clienthotel.id=reserverchambre.idClientHotel
AND roomservice.idReserverchambre=reserverchambre.id
AND roomservice.idCommanderestaurant=commanderestaurant.id
AND commanderestaurant.id=contenirplat.idCommandeRestaurant
AND contenirplat.idPlat=plat.id
AND reserverchambre.id=16
 
 
UNION  SELECT sum(contenirboisson.quantite*boisson.prix)AS Boisson
FROM clienthotel,reserverchambre,roomservice,commanderestaurant,contenirboisson,boisson
 
WHERE clienthotel.id=reserverchambre.idClientHotel
AND roomservice.idReserverchambre=reserverchambre.id
AND roomservice.idCommanderestaurant=commanderestaurant.id
AND commanderestaurant.id=contenirboisson.idCommandeRestaurant
AND contenirboisson.idBoisson=boisson.id
AND reserverchambre.id=16
 
 
union SELECT  sum(contenirgateau.nombreGateau*gateaupatisserie.prix) AS Gateaux 
 
FROM clienthotel,reserverchambre,roomservice,commanderestaurant,contenirgateau,gateaupatisserie
 
WHERE clienthotel.id=reserverchambre.idClientHotel
AND roomservice.idReserverchambre=reserverchambre.id
AND roomservice.idCommanderestaurant=commanderestaurant.id
AND commanderestaurant.id=contenirgateau.idCommandeRestaurant
AND contenirgateau.idGateauPatisserie=gateaupatisserie.id
 
AND reserverchambre.id=16
bingo06260 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/11/2011, 09h27   #4
Expert Confirmé
 
Avatar de Maljuna Kris
 
Homme Avcxjo MoKo
Retraité
Inscription : novembre 2005
Messages : 2 530
Détails du profil
Informations personnelles :
Nom : Homme Avcxjo MoKo
Âge : 60

Informations professionnelles :
Activité : Retraité
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : novembre 2005
Messages : 2 530
Points : 3 523
Points : 3 523
Je te propose cette requête normalisée
Code MySQL :
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
SELECT sr1.Plats, sr2.Boisson, sr3.Gateaux
FROM
(SELECT cli.id, sum(cp.nombrePlat*p.prix)AS Plats
FROM clienthotel cli
INNER JOIN reserverchambre rc 
    ON cli.id=rc.idClientHotel
INNER JOIN roomservice rs 
    ON rs.idReserverchambre=rc.id
INNER JOIN commanderestaurant cr 
    ON rs.idCommanderestaurant=cr.id
INNER JOIN contenirplat cp 
    ON cr.id=cp.idCommandeRestaurant
INNER JOIN plat p 
    ON cp.idPlat=p.id
WHERE rc.id=16) AS sr1
 
INNER JOIN
 
(SELECT cli.id, sum(cb.quantite*b.prix)AS Boisson
FROM clienthotel cli
INNER JOIN reserverchambre rc 
    ON cli.id=rc.idClientHotel
INNER JOIN roomservice rs 
    ON rs.idReserverchambre=rc.id
INNER JOIN commanderestaurant cr 
    ON rs.idCommanderestaurant=cr.id
INNER JOIN contenirboisson cb 
    ON cr.id=cb.idCommandeRestaurant
INNER JOIN boisson b 
    ON cp.idPlat=b.id
WHERE rc.id=16) AS sr2
    ON sr1.id=sr2.id
 
INNER  JOIN
 
(SELECT  cli.id, sum(cg.nombreGateau*gp.prix) AS Gateaux 
 FROM clienthotel cli
INNER JOIN reserverchambre rc 
    ON cli.id=rc.idClientHotel
INNER JOIN roomservice rs 
    ON rs.idReserverchambre=rc.id
INNER JOIN commanderestaurant cr 
    ON rs.idCommanderestaurant=cr.id
INNER JOIN contenirgateau cg 
    ON cr.id=cg.idCommandeRestaurant
INNER JOIN gateaupatisserie gp 
    ON cp.idPlat=gp.id
WHERE rc.id=16) AS sr3 
    ON sr3.id=sr1.id
__________________
Kie lumo eksistas ankaŭ ombro troviĝas. L.L. Zamenhof
articles : Comment émuler un tableau croisé [quasi] dynamique
et : Une énigme mathématique résolue avec MySQL
recommande l'utilisation de PDO (PHP5 Data Objects)
Maljuna Kris est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 07h57.


 
 
 
 
Partenaires

Hébergement Web