IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Requêtes MySQL Discussion :

Formulation d'une requête de statistique sur 3 tables différentes


Sujet :

Requêtes MySQL

  1. #1
    Candidat au Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2014
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Mars 2014
    Messages : 1
    Points : 2
    Points
    2
    Par défaut Formulation d'une requête de statistique sur 3 tables différentes
    Bonjour à tous
    j'ai une préoccupation depuis plusieurs jours que je n'arrive pas resoudre et je voudrais que vous m'aidiez
    en fait je voudrais formualer une requete pour savoir les statistiques des mouvements par type_document
    et les mouvements constituent les entrees et les sorties de documents d'une archive
    voici mes tables
    document(id_doc,date_creation,#id_type_doc)
    type_document(id_type_doc,lib_type_doc)
    type_mouvement(id_type_mouv,lib_type_mouv)
    mouvement(id_mouv,date_mouv,#id_doc,#id_type_mouv)
    je voudrais les statistiques de entrées et sorties par type_de_document que je dois entrer dans un formulaire
    je vous prie de m'aider à le résoudre
    Code html : 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
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    <?php
    include('../bd_connect.php');
    $req = $base->prepare('SELECT * FROM type_document');
    $req->execute();
    if(isset($_POST['rechercher']))
    {
        //formulation des requete de selection des mouvements//
        $reket = $base->prepare('SELECT * FROM mouvement,document,type_document,type_mouvement WHERE type_document.id_type_doc = document.id_type_doc AND type_mouvement.id_type_mouv = mouvement.id_type_mouv AND document.id_doc = mouvement.id_doc AND type_document.id_type_doc = :id_type_doc');
        $reket->bindvalue('id_type_doc',$_POST['id_type_doc'],PDO::PARAM_INT);
        $reket->execute();
        $tab = $reket->fetchAll();
        $count_total = $reket->rowCount();
        //formulation des requete de pour la liste des entree et sorties //
        $reket1 = $base->prepare('SELECT * FROM mouvement,document,type_document,type_mouvement WHERE type_document.id_type_doc = document.id_type_doc AND type_mouvement.id_type_mouv = mouvement.id_type_mouv AND document.id_doc = mouvement.id_doc AND type_mouvement.lib_type_mouv = "ENTREE" AND type_document.id_type_doc = :id_type_doc ');
        $reket1->bindvalue('id_type_doc',$_POST['id_type_doc'],PDO::PARAM_INT);
        $reket1->execute();
        $tab1 = $reket1->fetchAll();
        $count_entree = $reket1->rowCount();
        if($count_entree>0)
        {
            $pourcentage_entree = ($count_entree / $count_total)*100;
        }
        $reket2 = $base->prepare('SELECT * FROM mouvement,document,type_document,type_mouvement WHERE type_document.id_type_doc = document.id_type_doc AND type_mouvement.id_type_mouv = mouvement.id_type_mouv AND document.id_doc = mouvement.id_doc AND type_mouvement.lib_type_mouv =  = "SORTIE" AND type_document.id_type_doc = :id_type_doc ');
        $reket2->bindvalue('id_type_doc',$_POST['id_type_doc'],PDO::PARAM_INT);
        $reket2->execute();
        $tab2 = $reket2->fetchAll();
        //calcul du nombre de sortie de la requet//
     
        $count_sortie = $reket2->rowCount();
        if($count_sortie>0)
        {
            $pourcentage_sortie = ($count_sortie /$count_sortie) * 100;
        }
    }
    ?>
    <!DOCTYPE>
    <html>
    	<head>
    		<title>statistisques des mouvements par type de document</title>
     
    	</head>
    	<body>
    		<form method="POST" align="center"  >
    		<fieldset>
    		    <select name="id_type_doc">
    			  <?php
                               include('../bd_connect.php');
                               $tablo=$req->fetchAll();
                               foreach($tablo as $tablo)
                               {
                                       ?>
    				  <option value="<?php echo $tablo['id_type_doc']; ?>"> <?php echo strtoupper($tablo['lib_type']);?></option>
    				   <?php
                               }
                                       
                              ?>
    			</select>
    			<input type="submit" value="rechercher" name="rechercher" >
    		</fieldset>
    		</form>
     
    	    <?php   
                    if((isset($tab) AND $tab>0) AND isset($tab1) AND $tab1>0 AND isset($tab2) AND $tab2>0)
                    {
                    
                    ?>
    		<table border="1">
    		<tr>
     
    			 <th>Type</th>
    			 <th>ENTREES</th>
    			 <th>SORTIE</th>
    			 <th>STATISTIQUES ENTREES</th>
    			 <th>STATISTQUES SORTIES</th>
    		</tr>
    		<?php
                    foreach($tab as $tab)
                    { 
                               
                     
                            ?>
    		<tr>
     
    			<td><?php echo $tab['lib_type'];?></td>
    			<td><?php echo $count_entree;?></td>
    			<td><?php echo $count_sortie; ?></td>
    			<td><?php echo $pourcentage_entree." %"; ?></td>
    			<td><?php echo $pourcentage_sortie." %";  ?></td>
    		</tr>
    		<?php
                       }
                    
                    ?>
    		</table>
    		<?php
                    }
              ?>
       </body>
    </html>

  2. #2
    Modérateur
    Avatar de escartefigue
    Homme Profil pro
    bourreau
    Inscrit en
    Mars 2010
    Messages
    10 136
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : bourreau
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 10 136
    Points : 38 909
    Points
    38 909
    Billets dans le blog
    9
    Par défaut
    Bonjour,

    Voici mon jeu d'essai
    Code : 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
    with YD_TYDOC (YD_id, YD_lib) as
        (select 1, 'livre'
         union all
         select 2, 'CD'
         union all 
         select 3, 'DVD'
        )
       , DO_DOC(DO_id, YD_id) as
        (select 1, 1         
         union all
         select 2, 1         
         union all
         select 3, 2         
         union all
         select 4, 1         
         union all
         select 5, 3         
         union all
         select 6, 3         
        )
       , MV_MOUV(MV_id, MV_date, DO_id, YM_id) as
        (select 1, '2019-02-05', 3, 1   
         union all
         select 2, '2019-02-05', 5, 2   
         union all
         select 3, '2019-02-08', 1, 2   
         union all
         select 4, '2019-02-09', 5, 1   
         union all
         select 5, '2019-02-10', 2, 1   
         union all
         select 6, '2019-02-12', 2, 1   
         union all
         select 7, '2019-02-18', 4, 2   
        ) 
       , YM_TYMV(YM_id, YM_lib) as
        (select 1, 'entrée'   
         union all
         select 2, 'sortie')
    la requête
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    select YM.YM_lib
         , YD.YD_lib
         , count(*)
    from YM_TYMV as YM
    inner join MV_MOUV  as MV
       on MV.YM_id = YM.YM_id
    inner join DO_DOC   as DO  
       on DO.DO_id = MV.DO_id
    inner join YD_TYDOC as YD
       on YD.YD_id=DO.YD_id  
    group by YM.YM_lib
           , YD.YD_lib
    et le résultat obtenu
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    entrée	livre	2
    entrée	CD	1
    entrée	DVD	1
    sortie	livre	2
    sortie	DVD	1

  3. #3
    Expert éminent sénior Avatar de Artemus24
    Homme Profil pro
    Agent secret au service du président Ulysses S. Grant !
    Inscrit en
    Février 2011
    Messages
    6 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Agent secret au service du président Ulysses S. Grant !
    Secteur : Finance

    Informations forums :
    Inscription : Février 2011
    Messages : 6 381
    Points : 19 065
    Points
    19 065
    Par défaut
    Salut à tous.

    Quelque chose dans ce genre là, peut-être :
    Code : 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
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    --------------
    START TRANSACTION
    --------------
     
    --------------
    DROP DATABASE IF EXISTS `base`
    --------------
     
    --------------
    CREATE DATABASE IF NOT EXISTS `base`
            DEFAULT CHARACTER SET `latin1`
            DEFAULT COLLATE       `latin1_general_ci`
    --------------
     
    --------------
    DROP TABLE IF EXISTS `type_document`
    --------------
     
    --------------
    create table `type_document`
    ( `id_type_doc`   integer  unsigned  not null auto_increment primary key,
      `lib_type_doc`  varchar(255)       not null
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    INSERT INTO `type_document` (`lib_type_doc`) VALUES
      ('type document N°1'),('type document N°2'),('type document N°3')
    --------------
     
    --------------
    select * from `type_document`
    --------------
     
    +-------------+-------------------+
    | id_type_doc | lib_type_doc      |
    +-------------+-------------------+
    |           1 | type document N°1 |
    |           2 | type document N°2 |
    |           3 | type document N°3 |
    +-------------+-------------------+
    --------------
    DROP TABLE IF EXISTS `document`
    --------------
     
    --------------
    create table `document`
    ( `id_doc`         integer  unsigned  not null auto_increment primary key,
      `date_creation`  date               not null,
      `id_type_doc`    integer  unsigned  not null,
      CONSTRAINT `FK_TYPE_DOCUMENT` FOREIGN KEY (`id_type_doc`) REFERENCES `type_document` (`id_type_doc`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    INSERT INTO `document` (`date_creation`,`id_type_doc`) VALUES
      ('2019-01-01', 1), ('2019-01-01', 2), ('2019-01-01', 3),
      ('2019-02-01', 1), ('2019-02-01', 2), ('2019-02-01', 3)
    --------------
     
    --------------
    select * from `document`
    --------------
     
    +--------+---------------+-------------+
    | id_doc | date_creation | id_type_doc |
    +--------+---------------+-------------+
    |      1 | 2019-01-01    |           1 |
    |      2 | 2019-01-01    |           2 |
    |      3 | 2019-01-01    |           3 |
    |      4 | 2019-02-01    |           1 |
    |      5 | 2019-02-01    |           2 |
    |      6 | 2019-02-01    |           3 |
    +--------+---------------+-------------+
    --------------
    DROP TABLE IF EXISTS `type_mouvement`
    --------------
     
    --------------
    create table `type_mouvement`
    ( `id_type_mouv`   integer  unsigned  not null auto_increment primary key,
      `lib_type_mouv`  varchar(255)       not null
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    INSERT INTO `type_mouvement` (`lib_type_mouv`) VALUES  ('Entrée'),('Sortie')
    --------------
     
    --------------
    select * from `type_mouvement`
    --------------
     
    +--------------+---------------+
    | id_type_mouv | lib_type_mouv |
    +--------------+---------------+
    |            1 | Entrée        |
    |            2 | Sortie        |
    +--------------+---------------+
    --------------
    DROP TABLE IF EXISTS `mouvement`
    --------------
     
    --------------
    create table `mouvement`
    ( `id_mouv`       integer  unsigned  not null auto_increment primary key,
      `date_mouv`     date               not null,
      `id_doc`        integer  unsigned  not null,
      `id_type_mouv`  integer  unsigned  not null,
      CONSTRAINT `FK_DOCUMENT`       FOREIGN KEY (`id_doc`)       REFERENCES `document`       (`id_doc`)       ON DELETE CASCADE ON UPDATE CASCADE,
      CONSTRAINT `FK_TYPE_MOUVEMENT` FOREIGN KEY (`id_type_mouv`) REFERENCES `type_mouvement` (`id_type_mouv`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB
      DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
      ROW_FORMAT=COMPRESSED
    --------------
     
    --------------
    INSERT INTO `mouvement` (`date_mouv`,`id_doc`,`id_type_mouv`) VALUES
      ('2019-01-01', 1, 1),('2019-01-18', 1, 2),
      ('2019-01-23', 1, 1),('2019-01-27', 1, 2),
     
      ('2019-01-01', 2, 1),('2019-01-18', 2, 2),
      ('2019-02-01', 2, 1),('2019-02-07', 2, 2),
     
      ('2019-01-01', 3, 1),('2019-01-18', 3, 2),
      ('2019-01-23', 3, 1),('2019-01-27', 3, 2),
      ('2019-02-01', 3, 1),('2019-02-07', 3, 2),
     
      ('2019-02-01', 1, 1),('2019-02-18', 1, 2),
      ('2019-02-23', 1, 1),('2019-02-27', 1, 2),
      ('2019-02-01', 1, 1),('2019-02-07', 1, 2),
     
      ('2019-02-01', 2, 1),('2019-02-18', 2, 2),
      ('2019-02-23', 2, 1),('2019-02-27', 2, 2),
      ('2019-02-01', 2, 1),('2019-02-07', 2, 2),
     
      ('2019-02-01', 3, 1),('2019-02-18', 3, 2),
      ('2019-02-01', 3, 1),('2019-02-07', 3, 2)
    --------------
     
    --------------
    select * from `mouvement`
    --------------
     
    +---------+------------+--------+--------------+
    | id_mouv | date_mouv  | id_doc | id_type_mouv |
    +---------+------------+--------+--------------+
    |       1 | 2019-01-01 |      1 |            1 |
    |       2 | 2019-01-18 |      1 |            2 |
    |       3 | 2019-01-23 |      1 |            1 |
    |       4 | 2019-01-27 |      1 |            2 |
    |       5 | 2019-01-01 |      2 |            1 |
    |       6 | 2019-01-18 |      2 |            2 |
    |       7 | 2019-02-01 |      2 |            1 |
    |       8 | 2019-02-07 |      2 |            2 |
    |       9 | 2019-01-01 |      3 |            1 |
    |      10 | 2019-01-18 |      3 |            2 |
    |      11 | 2019-01-23 |      3 |            1 |
    |      12 | 2019-01-27 |      3 |            2 |
    |      13 | 2019-02-01 |      3 |            1 |
    |      14 | 2019-02-07 |      3 |            2 |
    |      15 | 2019-02-01 |      1 |            1 |
    |      16 | 2019-02-18 |      1 |            2 |
    |      17 | 2019-02-23 |      1 |            1 |
    |      18 | 2019-02-27 |      1 |            2 |
    |      19 | 2019-02-01 |      1 |            1 |
    |      20 | 2019-02-07 |      1 |            2 |
    |      21 | 2019-02-01 |      2 |            1 |
    |      22 | 2019-02-18 |      2 |            2 |
    |      23 | 2019-02-23 |      2 |            1 |
    |      24 | 2019-02-27 |      2 |            2 |
    |      25 | 2019-02-01 |      2 |            1 |
    |      26 | 2019-02-07 |      2 |            2 |
    |      27 | 2019-02-01 |      3 |            1 |
    |      28 | 2019-02-18 |      3 |            2 |
    |      29 | 2019-02-01 |      3 |            1 |
    |      30 | 2019-02-07 |      3 |            2 |
    +---------+------------+--------+--------------+
    --------------
    select      td.lib_type_doc,
                tm.lib_type_mouv,
                count(*) as nbre
     
          from  `mouvement`      as mv
     
    inner join  `type_mouvement` as tm
            on  tm.id_type_mouv  = mv.id_type_mouv
     
    inner join  `document`       as dc
            on  dc.id_doc        = mv.id_doc
     
    inner join  `type_document`  as td
            on  td.id_type_doc   = dc.id_type_doc
     
      group by  td.lib_type_doc, tm.lib_type_mouv
    --------------
     
    +-------------------+---------------+------+
    | lib_type_doc      | lib_type_mouv | nbre |
    +-------------------+---------------+------+
    | type document N°1 | Entrée        |    5 |
    | type document N°2 | Entrée        |    5 |
    | type document N°3 | Entrée        |    5 |
    | type document N°1 | Sortie        |    5 |
    | type document N°2 | Sortie        |    5 |
    | type document N°3 | Sortie        |    5 |
    +-------------------+---------------+------+
    --------------
    COMMIT
    --------------
     
    Appuyez sur une touche pour continuer...
    @+
    Si vous êtes de mon aide, vous pouvez cliquer sur .
    Mon site : http://www.jcz.fr

Discussions similaires

  1. Réponses: 5
    Dernier message: 19/05/2015, 01h38
  2. [AC-2003] Traduction en VBA d'une requête Sélection SQL sur plusieurs tables
    Par Pucho Faritas dans le forum VBA Access
    Réponses: 9
    Dernier message: 29/10/2009, 10h32
  3. [SQL] Formulation d'une requête UPDATE
    Par yopuke dans le forum Langage SQL
    Réponses: 3
    Dernier message: 10/05/2006, 12h29
  4. Une requête renvoyant EOF sur serveur (fonctionne ailleur)
    Par Tardiff Jean-François dans le forum ASP
    Réponses: 2
    Dernier message: 15/11/2005, 08h52
  5. problème de formulation d'une requête
    Par seiryujay dans le forum Langage SQL
    Réponses: 4
    Dernier message: 20/08/2004, 17h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo