选择最大且不同的查询 [英] select query with max and distinct

查看:429
本文介绍了选择最大且不同的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家,

我需要在MySQL中使用qeuery,

那里有一个表 tmpTable ,其中包含以下列-- id(bigint),inq_id(bigint),date(date)

假设表中有4行.

id inq_id日期
1 11 11/3/2011
2 11 15/3/2011
3 12 23/5/2011
5 12 25/6/2011

我想要结果与最大日期不同的inq_id.
如果有2个inq_id为11,则选择具有max(date)的inq_id.

根据上表,我想要类似
的结果
2 11 15/3/2011
5 12 25/6/2011

请帮助我

感谢adv.

Hi Expert,

I need on qeuery in MYSql,

there one table tmpTable with following columns -id(bigint),inq_id(bigint),date(date)

suppose there are 4 rows in table.

id inq_id date
1 11 11/3/2011
2 11 15/3/2011
3 12 23/5/2011
5 12 25/6/2011

i want result as distinct inq_id with max date.
IF there are 2 inq_id as 11, then it select inq_id which have max(date).

According above table,i want result like

2 11 15/3/2011
5 12 25/6/2011

please help me

Thanks in adv.

推荐答案

尝试:
SELECT m.* From MyTable m
INNER JOIN
   (SELECT inq_id, MAX([date]) AS [date] FROM myTable GROUP by inq_id) g
   ON m.inq_id = g.inq_id AND m.[date] = g.[date]
   ORDER BY iD ASC


这篇关于选择最大且不同的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆