[英]How to get(select) the rows with max value of a particular column using grouping
I have a table with duplicate data of books issued by students. 我有一张桌子,上面有学生们发行的书籍的重复数据。 Students can issue books(one book at a time in this case).
学生可以发行书籍(在这种情况下一次发行一本书)。 SQL for the table trn_books:
表trn_books的SQL:
CREATE TABLE IF NOT EXISTS `trn_books` (
`tb_id` int(11) NOT NULL,
`tb_roll` varchar(50) NOT NULL,
`tb_isbn` varchar(50) NOT NULL,
`tb_date_issue` varchar(100) NOT NULL,
`tb_date_return` varchar(100) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
INSERT INTO `trn_books` (`tb_id`, `tb_roll`, `tb_isbn`, `tb_date_issue`, `tb_date_return`) VALUES
(1, 'L23', '1234441225', '2017-01-10', '2017-01-20'),
(2, 'L54', '1225565412', '2017-01-12', '2017-01-22'),
(3, 'L23', '1225565412', '2017-01-22', '2017-01-31'),
(4, 'L24', '1225565412', '2017-02-01', '2017-02-11'),
(5, 'L23', '1225565412', '2017-02-15', '2017-02-25');
ALTER TABLE `trn_books`
ADD PRIMARY KEY (`tb_id`);
Record storing in this way: Query: SELECT * FROM trn_books
记录存储方式如下: 查询:SELECT * FROM
trn_books
Now I want to track the last activity of students using grouping by their tb_roll. 现在,我想根据学生的tb_roll分组来跟踪他们的上一次活动。 As we see that, the student with roll number L23 issued books three times where the last activity was:(according to max value of tb_date_return).
正如我们所看到的,卷号为L23的学生在最后一次活动是(根据tb_date_return的最大值)的情况下发行了三本书。 (5,L23,1225565412, 2017-02-15,2017-02-25).
(5,L23,1225565412,2017-02-15,2017-02-25)。 If I group by the table I am getting :
如果我按桌子分组,我会得到:
Query: SELECT * FROM trn_books
group by tb_roll 查询:SELECT * FROM
trn_books
组,按tb_roll
But I need the result of the students' latest activity(max value of tb_date_return). 但是我需要学生最近活动的结果(tb_date_return的最大值)。 Desire result should be tb_id's-->5,4,2.
期望结果应为tb_id's-> 5,4,2。
But I am getting tb_id's->1,4,2 How to achieve it by mysql query. 但是我正在获取tb_id's-> 1,4,2如何通过mysql查询实现它。
Thank you for your help... 谢谢您的帮助...
You want the max date returned for any given tb_roll? 您是否希望为任何给定的tb_roll返回最大日期?
select tb_roll, max(tb_date_return) as max_return
from trn_books
group by tb_roll
This is normally accomplished using a join
and group by
: 通常使用
join
和group by
来完成:
select b.*
from trn_books b join
(select tb_roll, max(tb_date_return) as max_tb_date_return
from trn_books
group by tb_roll
) b2
on b2.tb_roll = b.tb_roll and
b2.max_tb_date_return = b.tb_date_return;
Another common method is a correlated subquery: 另一个常见的方法是相关子查询:
select b.*
from trn_books b
where b.tb_date_return = (select max(b2.tb_date_return)
from trn_books b2
where b2.tb_roll = b.tb_roll
);
This is a max group wise problem. 这是最大的团体明智的问题。 You need to solve this with a delivered table.
您需要使用交付的表格解决此问题。
Query 询问
SELECT
trn_books.*
FROM (
SELECT
tb_roll
, MAX(tb_date_return) as max_return
FROM
trn_books
GROUP BY
tb_roll
ORDER BY
tb_roll ASC
)
AS
tb_roll_max
INNER JOIN
trn_books
ON
tb_roll_max.tb_roll = trn_books.tb_roll
AND
tb_roll_max.max_return = trn_books.tb_date_return
ORDER BY
trn_books.tb_id DESC
Result 结果
tb_id tb_roll tb_isbn tb_date_issue tb_date_return
------ ------- ---------- ------------- ----------------
5 L23 1225565412 2017-02-15 2017-02-25
4 L24 1225565412 2017-02-01 2017-02-11
2 L54 1225565412 2017-01-12 2017-01-22
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.