cs_piep14
Messages postés345Date d'inscriptionlundi 15 mars 2004StatutMembreDernière intervention13 mai 2011
-
26 avril 2006 à 19:21
cs_piep14
Messages postés345Date d'inscriptionlundi 15 mars 2004StatutMembreDernière intervention13 mai 2011
-
27 avril 2006 à 10:16
Bonsoir, j'aurais une simple question pour vous :
Je voudrais joindre des champs de tables différrentes. J'ai pour le moment la requête :
Code :
SELECT * FROM `tarifs` WHERE `produit`='Gazoil'
Ca c'est la requête dans la table tarifs ! Mais je souhaite afficher des infos de la table stations en ajoutant :
Code :
cs_piep14
Messages postés345Date d'inscriptionlundi 15 mars 2004StatutMembreDernière intervention13 mai 2011 26 avril 2006 à 22:23
Voila :
SELECT * FROM `tarifs` INNER JOIN `stations` ON tarifs.id_station = stations.id_station WHERE tarifs.produit='Gasoil' AND stations.CP_station LIKE '$_GET[NumDept]%'
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 201137 27 avril 2006 à 00:31
salut
Pour les quotes j'en met que pour les chaine de caractere et pas pour les champs
car moi je travaille en general avec sqlserver
et la il faut mettre [...] c'est peut être remplacé par '...' pour la table et les noms de champs en mysql peut préciser ce point.
SELECT Lieu ,max(date) as max_date FROM `tarifs` INNER JOIN `stations` ON tarifs.id_station = stations.id_station WHERE tarifs.produit='Gasoil' AND stations.CP_station LIKE '$_GET[NumDept]%' group by Lieu
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 201137 26 avril 2006 à 21:07
Salut
lis les messages.
Il manque la liaison entre tes deux tables.
soit dans le inner join cf message precedent
soit dans la clause where cf message précédent
Il faut un champ clef et un champ cle etrangere
exemple sur ton modele
SELECT * FROM `tarifs`,`stations`
WHERE tarifs.id_station = stations.id_staiton
AND `produit`='Gazoil'
AND `stations.CP_station` LIKE '$_GET[NumDept]%'
et le tour est jouer
ou
SELECT * FROM `tarifs`
INNER JOIN `stations`
ON tarifs.id_station = stations.id_staiton
WHERE `produit`='Gazoil'
AND `stations.CP_station` LIKE '$_GET[NumDept]%'
cs_piep14
Messages postés345Date d'inscriptionlundi 15 mars 2004StatutMembreDernière intervention13 mai 2011 26 avril 2006 à 21:24
Je comprends pas :
mysql_query("SELECT * FROM `tarifs` INNER JOIN `stations` ON tarifs.id_station = stations.id_station WHERE `tarifs.produit`='Gazoil' AND `stations.CP_station` LIKE '$_GET[NumDept]%'");
Avec $_GET[NumDept] => 35
Voici mes deux tables :
CREATE TABLE `stations` (
`id_station` int(11) NOT NULL auto_increment,
`nom_station` varchar(50) NOT NULL,
`adresse_station` varchar(100) NOT NULL,
`CP_station` varchar(5) NOT NULL,
`ville_station` varchar(30) NOT NULL,
`paiement` enum('Oui','Non') NOT NULL,
PRIMARY KEY (`id_station`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
CREATE TABLE `tarifs` (
`id` int(11) NOT NULL auto_increment,
`id_station` int(11) NOT NULL,
`produit` enum('Gazoil','Sans Plomb 95','Sans Plomb 98','GPL') NOT NULL,
`tarif` varchar(10) NOT NULL,
`date` date NOT NULL,
`heure` time NOT NULL,
`pseudo` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
Et je voudrais afficher que la date la plus récente selon la station
Ici, j'aimerais récuperer la station 1 et la station avec les dates les plus récentes
cs_piep14
Messages postés345Date d'inscriptionlundi 15 mars 2004StatutMembreDernière intervention13 mai 2011 27 avril 2006 à 10:03
En faisant ceci :
SELECT *,max(date) as max_date FROM `tarifs` INNER JOIN `stations` ON tarifs.id_station = stations.id_station WHERE tarifs.produit='Gasoil' AND stations.CP_station LIKE '$_GET[NumDept]%' group by stations.id_station
Il me renvoie bien 2 enregistrements mais pas avec la date la plus récente !