需要重写查询以在where子句中使用max函数

时间:2015-12-19 03:24:11

标签: mysql

此时正在使用的查询:

update metrics.time_created 
set cumu_count = (
    select count(*) 
    from perf_stats.time 
    where date(insert_datetime)='2015-12-18'
) 
where id=max(id);

我得到了"无效使用群组功能"在这里 - 我怎样才能重写这个以保持相同的逻辑?我必须更新metrics.time_created表中的最新行。

3 个答案:

答案 0 :(得分:1)

试试这个:

update metrics.time_created a
set cumu_count = (
  select count(*) 
  from perf_stats.time 
  where date(insert_datetime)='2015-12-18'
) 
where exists (
  select 1
  from (select max(id) as maxid from metrics.time_created) t
  where maxid = a.id
);

示例演示:http://sqlfiddle.com/#!9/6bc3cd/1

编辑:

根据评论,这里有改变

update metrics.time_created a

set cumu_count = 
(
  select count(*) 
  from perf_stats.time pt
  where exists
  (
    select 1
    from (select max(curr_date) as mcd from metrics.time_created) x
    where mcd = date(insert_datetime)
  )
) 

where exists 
(
  select 1
  from (select max(id) as maxid from metrics.time_created) t
  where maxid = a.id
);

示例演示:http://sqlfiddle.com/#!9/fcc91a/1

答案 1 :(得分:0)

你可以使用这样的自联接:

update metrics.time_created as t1
inner join (
    select max(id) as id 
    from metrics.time_created
) as t2 on t1.id = t2.id
set cumu_count = (
  select count(*) 
  from perf_stats.time 
  where date(insert_datetime)='2015-12-18'
)

答案 2 :(得分:0)

如果您尝试使用最大If My.Computer.FileSystem.DirectoryExists("C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319") Then 'do something End If 更新一行,则可以使用 Sub firstValueOfColumn() Dim dColumn As Long Cells.Select For Each tCell In Selection If tCell = 0.650833312 Then dColumn = tCell.Column tCell.Select If Cells(1, dColumn) = "" Then MsgBox Cells(1, dColumn).End(xlDown).Value Else MsgBox Cells(1, dColumn).Value End If Exit For End If Next End Sub id

order by