我正在尝试从表中选择除分组数据的最后一行以外的所有行。
+----+--------+--------+ +----+--------+--------+ | id | userID | amount | | id | userID | amount | +----+--------+--------+ +----+--------+--------+ | 1 | 20 | 400 | | 1 | 20 | 400 | +----+--------+--------+ +----+--------+--------+ | 2 | 20 | 200 | | 2 | 20 | 200 | +----+--------+--------+ => +----+--------+--------+ | 3 | 21 | 100 | | 3 | 21 | 100 | +----+--------+--------+ +----+--------+--------+ | 4 | 11 | 500 | | 4 | 11 | 500 | +----+--------+--------+ +----+--------+--------+ | 5 | 11 | 250 | | 6 | 21 | 50 | +----+--------+--------+ +----+--------+--------+ | 6 | 21 | 50 | +----+--------+--------+ | 7 | 20 | 100 | +----+--------+--------+ | 8 | 21 | 200 | +----+--------+--------+
我尝试使用查询
SELECT * FROM table WHERE userID != (SELECT MAX(userID) FROM table) GROUP BY userID
但是它只获取一行唯一的数据,即使还有更多的数据
答案 0 :(得分:1)
您没有聚集功能,因此不需要分组依据
SELECT *
FROM table
WHERE userID != (
SELECT MAX(userID) FROM table
)
在mysql版本<5.7(对于mysql版本> 5.7)(默认设置)下,使用group by会引起错误
答案 1 :(得分:0)
例如...
SELECT a.*
FROM my_table a
LEFT
JOIN
( SELECT MAX(id) id
FROM my_table
GROUP
BY userid
) b
ON b.id = a.id
WHERE b.id IS NULL