· 8 years ago · Aug 14, 2018, 03:56 PM
1Get MAX and MIN values along with their row id?
2(temp_hi id) 0, (temp_hi) 72, (temp_lo id) 2, (temp_low) 28
3
4(temp_hi id) 0, (temp_hi) 72, (temp_lo id) 2, (temp_low) 28
5
6SELECT * FROM data
7WHERE temp_hi = (SELECT MAX(temp_hi) FROM data)
8OR temp_lo = (SELECT MIN(temp_lo) FROM data);
9
10SELECT
11 (SELECT ID FROM temp ORDER BY temp_hi DESC LIMIT 1) AS max_temp_id,
12 MAX(temp_hi) AS max_temp,
13 (SELECT ID FROM temp ORDER BY temp_lo LIMIT 1) AS min_temp_id,
14 MIN(temp_lo) AS min_temp
15FROM temp
16
17CREATE TABLE IF NOT EXISTS `temp` (
18 `ID` int(11) NOT NULL,
19 `temp_hi` int(11) NOT NULL,
20 `temp_lo` int(11) NOT NULL,
21 PRIMARY KEY (`ID`)
22) ENGINE=MyISAM DEFAULT CHARSET=utf8;
23
24
25INSERT INTO `temp` (`ID`, `temp_hi`, `temp_lo`) VALUES
26(0, 72, 38),
27(1, 56, 33),
28(2, 67, 28);