Problème de requete

VaN
Mammouth du PHP | 1107 Messages

05 avr. 2010, 22:21

Bonjour,

Voici les 2 tables utilisées pour ma requête :
CREATE TABLE IF NOT EXISTS `labels` (
`label_id` int(11) NOT NULL AUTO_INCREMENT,
`label_date` date NOT NULL,
`label_nom` varchar(100) NOT NULL,
`label_limite` date NOT NULL,
`label_annee` year(4) NOT NULL,
`label_participations_max_je` tinyint(4) NOT NULL,
`label_statut` tinyint(4) NOT NULL,
PRIMARY KEY (`label_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

CREATE TABLE IF NOT EXISTS `labels_inscriptions` (
`inscription_id` int(11) NOT NULL AUTO_INCREMENT,
`inscription_label_id` int(11) NOT NULL,
`inscription_je_id` int(11) NOT NULL,
`inscription_dossier_partenaires` varchar(100) NOT NULL,
`inscription_dossier_cnje` varchar(100) NOT NULL,
`inscription_statut` tinyint(4) NOT NULL,
PRIMARY KEY (`inscription_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
La liaison entre les deux tables se fait entre le champ label_id de la table labels et le champ inscription_label_id de la table labels_inscriptions

Et voici la requête qui poste problème :
SELECT label_id, label_nom, label_participations_max_je, COUNT(inscription_id) AS nb_inscriptions FROM labels 
	LEFT JOIN labels_inscriptions ON inscription_label_id = label_id AND inscription_je_id = 1
	WHERE label_statut = 1
Je souhaiterai modifier ma requête pour ne pas récupérer les labels qui ont déjà un nombre d'inscriptions maximum, c'est à dire les labels dont le champ label_participations_max_je est = à COUNT(inscription_id) sur la table labels_inscriptions.

Puis-je faire cela en une seule requête ?

Mammouth du PHP | 881 Messages

06 avr. 2010, 19:25

Oui tu peux faire cela.

Voici ce qui devrait te permettre de limiter les résultats à ta convenance.
SELECT label_id, label_nom, label_participations_max_je, COUNT(inscription_id) AS nb_inscriptions FROM labels 
        LEFT JOIN labels_inscriptions ON inscription_label_id = label_id AND inscription_je_id = 1
        WHERE label_statut = 1 AND nb_inscriptions < 8
Soyez artisans de paix

VaN
Mammouth du PHP | 1107 Messages

08 avr. 2010, 13:07

C'est ce que j'avais essayé, de cette façon :
SELECT label_id, label_nom, label_participations_max_je, COUNT(inscription_id) AS nb_inscriptions FROM labels
        LEFT JOIN labels_inscriptions ON inscription_label_id = label_id AND inscription_je_id = 1
        WHERE label_statut = 1 AND nb_inscriptions < label_participations_max_je
puisque c'est le champ label_participations_max_je qui définie combien d'inscriptions sont possible par entités.

Mais cela me renvoie l'erreur suivante : Unknown column 'nb_inscriptions' in 'where clause'

Mammouth du PHP | 881 Messages

08 avr. 2010, 22:41

Pour activer le comptage, il faut faire un GROUP BY, mais avec cette commande, on perd WHERE, il faudra faire un INNER JOIN, je crois

Voici pour l'instant où j'en suis
SELECT label_id, label_nom, label_participations_max_je, COUNT( inscription_id ) AS nb_inscriptions
FROM labels
LEFT JOIN labels_inscriptions ON inscription_label_id = label_id
GROUP BY inscription_label_id
En combinant avec HAVING que j'oublie tout le temps, je crois que nous trouvons ta formule magique:
SELECT label_id, label_nom, label_participations_max_je, COUNT( inscription_id ) AS nb_inscriptions
FROM labels
LEFT JOIN labels_inscriptions ON inscription_label_id = label_id
GROUP BY inscription_label_id
HAVING nb_inscriptions >1
ORDER BY nb_inscriptions ASC
Soyez artisans de paix

VaN
Mammouth du PHP | 1107 Messages

13 avr. 2010, 11:29

En combinant avec HAVING que j'oublie tout le temps, je crois que nous trouvons ta formule magique:
SELECT label_id, label_nom, label_participations_max_je, COUNT( inscription_id ) AS nb_inscriptions
FROM labels
LEFT JOIN labels_inscriptions ON inscription_label_id = label_id
GROUP BY inscription_label_id
HAVING nb_inscriptions >1
ORDER BY nb_inscriptions ASC
Ca a l'air de marcher plutôt pas mal effectivement !

Il me reste un détail à régler : dans ces labels, ne récupérer que ceux où label_statut = 1.
Comment faire, si je ne peux plus me servir de WHERE ?

Mammouth du PHP | 881 Messages

13 avr. 2010, 18:29

tu peux confier la tâche à HAVING
Soyez artisans de paix