在MySQL上使用select函数

  • 本文关键字:select 函数 MySQL mysql
  • 更新时间 :
  • 英文 :


对于一个家庭作业,我需要有一个查询来显示t.idEtape的最大值,该值=每个idEtapeidEtape,同时还显示nomEtapeLivrable的值,同时也不使用分组函数。

我已经想了一段时间了,但我快要放弃了。我现在有一些这样的,但这不起作用:

SELECT `idEtape`,`idProjet`,`nomEtape`, `Livrable`
FROM `jegere.EtapexProjet` , `jegere.Etape`
where `idEtape` = (select max(`t.idEtape`) from `jegere.EtapexProjet`)

有人能提供一些启示吗?

感谢

INSERT INTO `jegere.Etape` (`idEtape` , `nomEtape`, `Livrable`) VALUES
(1,'Démarrage','Définition du base de projet (objectifs, chef du projet)'),
(2,'Prébision','Planification du projet (périmètre, activités, ressources req)'),
(3,'Réalisation','Exécution du plan du projet'),
(4,'Surveillance et Maîtrise','Rapport de performance'),
(5,'Clôture','Document de clôture du projet');
INSERT INTO `jegere.EtapexProjet` (`t.idEtape`,`idProjet`, `dateDebut` , `dateFin`) VALUES
(1,1,'2011-07-01','2011-09-01'),
(2,1,'2011-09-02','2011-11-30'),
(3,1,'2011-12-01','2012-07-07'),
(4,1,'2012-07-08',NULL),
(1,2,'2012-05-01','2012-05-10'),
(2,2,'2012-05-11','2012-06-01'),
(3,2,'2012-06-02','2012-07-01'),
(4,2,'2012-07-01','2012-07-21'),
(5,2,'2012-07-22','2012-07-23'),
(1,3,'2011-11-01','2012-01-20'),
(2,3,'2012-01-21','2012-04-01'),
(3,3,'2012-04-02', NULL);

(部分答案;和课程(

第1课:

从两个表中提取时,不要使用旧语法:

FROM a, b
WHERE a.x = b.y  -- how the tables are related

相反,使用新的语法

FROM a
JOIN b  ON a.x = b.y  -- how the tables are related

第2课:

如果去掉a.x = b.y,则得到一个CROSS JOIN,其中查询返回两个表中的所有行对。(交叉连接很少使用。(

第3课:

当JOINing表时,限定每个列,以便读者知道哪个表包含每个列。我喜欢用";别名";使鉴定不那么庞大:

SELECT p.`idEtape`, e.`idProjet`, e.`nomEtape`, '.`Livrable`
FROM `jegere.EtapexProjet` AS p
JOIN `jegere.Etape` AS e
ON ...
...

第4课:在这里提问时,请为每个表提供SHOW CREATE TABLE,这样我们就可以看到数据类型、索引等。

最新更新