使用此脚本,我获得该列的最大值。
SELECT p.ploegnaam, MAX(k.punten) AS punten
FROM ploeg p,
klassement k
WHERE p.id = k.ploeg
但是当我有两个具有相同值的值时,它们是最大值。我只是 得到第一个。
当该列的最大值为20且该列中有3个值为20时,如何获取所有这些值而不仅仅是该列的第一个值为20?
db script
/*Table structure for table `klassement` */
DROP TABLE IF EXISTS `klassement`;
CREATE TABLE `klassement` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`punten` int(2) NOT NULL,
`ploeg` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ploeg` (`ploeg`),
CONSTRAINT `klassement_ibfk_1` FOREIGN KEY (`ploeg`) REFERENCES `ploeg` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;
/*Data for the table `klassement` */
insert into `klassement`(`id`,`punten`,`ploeg`) values
(14,7,29),
(15,7,30),
(16,1,31),
(17,0,32),
(18,0,34),
(19,1,36),
(20,0,40),
(21,0,41);
/*Table structure for table `ploeg` */
DROP TABLE IF EXISTS `ploeg`;
CREATE TABLE `ploeg` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`Ploegnaam` varchar(50) CHARACTER SET latin1 NOT NULL,
`Stadium` int(3) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `Stadium` (`Stadium`),
CONSTRAINT `ploeg_ibfk_1` FOREIGN KEY (`Stadium`) REFERENCES `stadium` (`id`),
CONSTRAINT `ploeg_ibfk_2` FOREIGN KEY (`Stadium`) REFERENCES `stadium` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8;
/*Data for the table `ploeg` */
insert into `ploeg`(`id`,`Ploegnaam`,`Stadium`) values
(29,'Club Brugge',41),
(30,'Cercle Brugge',41),
(31,'België',41),
(32,'belgiê',41),
(33,'belgië',41),
(34,'belgie',41),
(35,'belgiê',41),
(36,'Mùnchen',41),
(37,'mùnchen',41),
(38,'mùnchen',41),
(39,'mùnchen',41),
(40,'mùchuo',41),
(41,'ùùùù',41);
/*Table structure for table `stadium` */
DROP TABLE IF EXISTS `stadium`;
CREATE TABLE `stadium` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`Naam` varchar(50) CHARACTER SET latin1 NOT NULL,
`info` varchar(240) CHARACTER SET latin1 DEFAULT NULL,
`Adres` varchar(2048) CHARACTER SET latin1 NOT NULL,
`afbeelding` varchar(240) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*Data for the table `stadium` */
insert into `stadium`(`id`,`Naam`,`info`,`Adres`,`afbeelding`) values
(41,'Jan breydel','jan breydel in brugge','Jan Breydelstadion, Koning Leopold III-laan, Brugge, België','../uploads/volkswagen.jpg'),
(42,'jan','f','Kortrijk, België','../uploads/12042828_428221037364899_823789106058625890_n.jpg'),
(43,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(44,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(45,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(46,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(47,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(48,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(49,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(50,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(51,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
由于
答案 0 :(得分:0)
我认为您可以使用嵌套查询:
SELECT p.ploegnaam, k.punten AS punten
FROM ploeg p inner join klassement k on p.id = k.ploeg
WHERE k.punten = (select max(punten) from klassement )
答案 1 :(得分:0)
你可以用这个
select p.ploegnaam, k.punten
from ploeg p
inner join klassement k
on p.id = k.ploeg
inner join (SELECT p.ploegnaam, MAX(k.punten) AS max_punten
FROM ploeg p1
inner join klassement k1
on p1.id = k1.ploeg
) m
on p.id = m.ploegnaam and k.punten = m.max_punten;
从现在开始尝试不使用old-style-join
。