[英]MySQL is returning a different value then the max value in the sample code provided
在playertracker表中,我希望结果显示与具有最高值的行相关联的牌组名称,其中玩家ID(PID)等于给定值。 而是返回第一个牌组名称以及最大量。
create table players(
pid int(10),
pname varchar(20),
favcard varChar(20),
mstplayed varChar(20),
lestplayed varChar(20)
);
create table playertracker(
pid int(10),
deckName varchar(20),
amount int(10)
);
Insert into players
values(1, 'joe', 'swim', 'jump', 'fall'),(2, 'jane', 'up', 'jump', 'fall'),(3, 'jack', 'up', 'jump', 'fall'),
(4, 'joe', 'up', 'all', '5'),(5, 'joe', 'up', 'red', 'fall');
Insert into playertracker
values('1','jump','2'),('1','up','4'),('1','swim','9'),('1','fall','9'),
('2','jump','8'),('2','up','4'),('2','swim','1'),('2','fall','1'),
('3','jump','1'),('3','up','8'),('3','swim','9'),('3','fall','4'),
('4','jump','9'),('4','up','8'),('4','swim','1'),('4','fall','1'),
('5','jump','1'),('5','up','4'),('5','swim','4'),('5','fall','8'),
('6','jump','4'),('6','up','9'),('6','swim','1'),('6','fall','1');
select deckname, max(amount) from playertracker where pid = 1;
select deckname, max(amount) from playertracker where pid = 1 group by deckname;
这是查询但返回两行
select deckname, amount from playertracker where pid = 1
and amount = (select max(amount) from playertracker where pid = 1) ;
因为
swim 9
fall 9
所以
select deckname, amount from playertracker where pid = 1
and amount = (select max(amount) from playertracker where pid = 1)
limit 1 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.