我有这张桌子:
CREATE TABLE IF NOT EXISTS `Vars` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`code` varchar(15) NOT NULL,
`userID` bigint(20) NOT NULL,
`viewedT` bigint(20) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
我有这些价值观:
INSERT INTO `Vars` (`ID`, `code`, `userID`, `viewedT`) VALUES
(1, 'alex1234567', 1, 25),
(2, 'Dan1234567', 2, 15);
现在奇怪的是,我无法通过此查询获得正确的结果:
select min(`viewedT`), `code` From Vars
结果是:
min(`viewedT`) Code
15 alex1234567
编辑:我需要的是选择code
min(viewsT)
我确信我遗漏的语法中的某些东西确实会让我失望,但我需要知道我的错误并从中吸取教训。
答案 0 :(得分:2)
假设您要查找具有最小值的一行,请使用
select viewedT, code, id from vars
where viewedT= (select min(viewedT) from vars)
如果多行包含最小值,您将获得所有这些行。即如果两行的viewedT
值为15,那么你可以得到两者。
答案 1 :(得分:2)
select viewedT,Code from Vars
where viewedT=(select MIN(V.viewedT) from Vars as V)
它会给你以下结果
viewedT Code
15 Dan1234567