Mysql由两列组成,其中包含第三列的where条件和最大日期
我有一个表,其中包含user\u id、item\u id和last\u date作为列。最后一列类型为datetime 我需要通过对列user\u id和item\u id进行分组来检索上一个\u日期,条件是类型\u id类似于foo和very import,我需要在结果中包含所有列,因为在mytable中有更多列 以下是mytable结构:Mysql由两列组成,其中包含第三列的where条件和最大日期,mysql,sql,group-by,max,aggregate-functions,Mysql,Sql,Group By,Max,Aggregate Functions,我有一个表,其中包含user\u id、item\u id和last\u date作为列。最后一列类型为datetime 我需要通过对列user\u id和item\u id进行分组来检索上一个\u日期,条件是类型\u id类似于foo和very import,我需要在结果中包含所有列,因为在mytable中有更多列 以下是mytable结构: user_id item_id last_date type_id 129678 2 2019-02-17
user_id item_id last_date type_id
129678 2 2019-02-17 11:00:09 foo
129678 1 2019-02-17 11:00:15 foo
129678 2 2019-02-17 11:00:10 bar
129678 2 2019-02-17 11:00:10 bar
129678 2 2019-02-17 11:00:11 foo
129678 2 2019-02-17 11:00:15 foo
129678 1 2019-02-17 11:00:09 foo
129678 2 2019-02-17 11:00:14 bar
129678 2 2019-02-17 11:00:08 bar
129678 2 2019-02-17 11:00:11 foo
129678 2 2019-02-17 11:00:14 bar
129678 2 2019-02-17 11:00:10 foo
12a0d8 3 2019-02-17 11:00:08 foo
12a0d8 2 2019-02-17 11:00:12 foo
12a0d8 3 2019-02-17 11:00:08 bar
12a0d8 3 2019-02-17 11:00:12 bar
12a0d8 1 2019-02-17 11:00:10 foo
12a0d8 1 2019-02-17 11:00:11 bar
12a0d8 3 2019-02-17 11:00:14 foo
12a0d8 3 2019-02-17 11:00:12 foo
18ae98 2 2019-02-17 11:00:12 foo
18ae98 3 2019-02-17 11:00:07 bar
18ae98 1 2019-02-17 11:00:13 bar
18ae98 1 2019-02-17 11:00:14 foo
18ae98 2 2019-02-17 11:00:09 foo
18ae98 2 2019-02-17 11:00:13 foo
18ae98 3 2019-02-17 11:00:08 foo
18ae98 1 2019-02-17 11:00:12 foo
18ae98 3 2019-02-17 11:00:10 foo
18ae98 1 2019-02-17 11:00:12 foo
以下是我为此尝试的查询:
SELECT * FROM mytable
WHERE last_date IN (
SELECT MAX(last_date)
FROM mytable
WHERE type_id LIKE 'foo'
GROUP BY user_id, item_id
)
预期结果:
user_id item_id last_date type_id
129678 1 2019-02-17 11:00:15 foo
129678 2 2019-02-17 11:00:15 foo
12a0d8 1 2019-02-17 11:00:10 foo
12a0d8 2 2019-02-17 11:00:12 foo
12a0d8 3 2019-02-17 11:00:14 foo
18ae98 1 2019-02-17 11:00:14 foo
18ae98 2 2019-02-17 11:00:13 foo
18ae98 3 2019-02-17 11:00:10 foo
但是结果很奇怪!!例如,对于用户id返回具有相同值的多个项目id
编辑:
如果我使用这个查询,它将运行良好,但我必须指定一个间隔日期
SELECT * FROM mytable
WHERE type_id LIKE 'foo'
AND last_date > date_sub(curdate(), interval 2 day)
GROUP BY user_id, item_id
使用相关子查询
SELECT t1.* FROM mytable t1
WHERE last_date = (
SELECT MAX(last_date)
FROM mytable t2
WHERE t1.user_id=t2.user_id and t1.itemid=t2.itemid
and type_id LIKE 'foo'
)
下面是一种方法-
SELECT
from MyTable t1
INNER JOIN
(
SELECT user_id,
item_id,
max(last_date) AS lastdate
FROM MyTable
WHERE type_id='foo'
GROUP BY user_id,
item_id) t2
ON t1.user_id=t2.user_id
AND t1.item_id=t2.item_id
AND t1.last_date=t2.lastdate
您可以简单地将您关注的2列分组,然后获取maxlast_日期。您以前使用的子查询不会限制maxlast_日期,因为多个用户id和项目id组合可以应用于同一日期 下面是对示例数据的总结,它可以生成预期的结果,而无需编写额外的嵌套查询
declare @t table (
user_id nvarchar(15),
item_id int,
last_date datetime2,
type_id nvarchar(3)
);
insert into @t (user_id, item_id, last_date, type_id)
values
('129678', 2, '2019-02-17 11:00:09', 'foo'),
('129678', 1, '2019-02-17 11:00:15', 'foo'),
('129678', 2, '2019-02-17 11:00:10', 'bar'),
('129678', 2, '2019-02-17 11:00:10', 'bar'),
('129678', 2, '2019-02-17 11:00:11', 'foo'),
('129678', 2, '2019-02-17 11:00:15', 'foo'),
('129678', 1, '2019-02-17 11:00:09', 'foo'),
('129678', 2, '2019-02-17 11:00:14', 'bar'),
('129678', 2, '2019-02-17 11:00:08', 'bar'),
('129678', 2, '2019-02-17 11:00:11', 'foo'),
('129678', 2, '2019-02-17 11:00:14', 'bar'),
('129678', 2, '2019-02-17 11:00:10', 'foo'),
('12a0d8', 3, '2019-02-17 11:00:08', 'foo'),
('12a0d8', 2, '2019-02-17 11:00:12', 'foo'),
('12a0d8', 3, '2019-02-17 11:00:08', 'bar'),
('12a0d8', 3, '2019-02-17 11:00:12', 'bar'),
('12a0d8', 1, '2019-02-17 11:00:10', 'foo'),
('12a0d8', 1, '2019-02-17 11:00:11', 'bar'),
('12a0d8', 3, '2019-02-17 11:00:14', 'foo'),
('12a0d8', 3, '2019-02-17 11:00:12', 'foo'),
('18ae98', 2, '2019-02-17 11:00:12', 'foo'),
('18ae98', 3, '2019-02-17 11:00:07', 'bar'),
('18ae98', 1, '2019-02-17 11:00:13', 'bar'),
('18ae98', 1, '2019-02-17 11:00:14', 'foo'),
('18ae98', 2, '2019-02-17 11:00:09', 'foo'),
('18ae98', 2, '2019-02-17 11:00:13', 'foo'),
('18ae98', 3, '2019-02-17 11:00:08', 'foo'),
('18ae98', 1, '2019-02-17 11:00:12', 'foo'),
('18ae98', 3, '2019-02-17 11:00:10', 'foo'),
('18ae98', 1, '2019-02-17 11:00:12', 'foo');
select * from (
select USER_ID, item_id, MAX(last_date) as last_date, type_id
from @t where type_id = 'foo' group by user_id, item_id, type_id
) x
order by user_id, item_id;
您使用的是哪个MySQL版本?请指定预期的结果,表数据如上。@jarlh我使用的是两个版本:v5.1.47 dev,v5.7.19 prod。为了获得预期的结果,我更新了我的问题。我已使用临时解决方案编辑了我的问题。现在修复这两个版本!好的,如果我使用你的查询,seams工作正常,但是在我的数据库中,phpMyAdmin仍然有两倍/三倍相同的项\u id:\的值,我必须添加第二个条件t2。type\u id像'foo'@zaynulabadinthin:不,对不起,我已经用临时解决方案编辑了我的问题。我认为您在fiddle和服务器中的数据不一样,否则无法在fiddle上使用,但在服务器中无法使用。问题是,我无法与您共享数据,因为它是私有的:\sorrywell如果我使用您的查询,接缝工作正常,但在我的数据库中,使用phpMyAdmin时仍然存在双/三重接缝项\u id:\的值相同。是否可以对获得意外输出的数据进行采样。仅当您在回复中输入了更多特定的条目-user\u id、item\u id、last\u datethx,但“groupby”不在正确的位置时,查询才会返回重复项syntax@Peacefull感谢您指出out为我服务,因为我没有直接从查询窗口粘贴。我已提出where条款。当在正确的位置使用where子句运行时,查询确实提供了预期的结果。没问题,您的查询现在更好了,但是在我的第一个请求中,如果我对某些列使用SELECT,则我需要使用SELECT*返回所有列。这是我的问题…我添加了一个select*作为外部查询。我不知道为什么select*比单独选择列更可取。但是,发布的查询在建议的条件下给出了预期的结果。我尝试了您上次的更改,但它仅返回select列,而不返回my DB中的所有其他列。