数据原型:
两个节点,上报数据;节点通过网关接入,中间更换了网关(netmac1-->netmac2)
dataid,netmac,nodemac,updatetime,data1,data2
1      net1    node1   1           1    1
2      net1    node2   2           1    1
3      net1    node1   3           1    1
4      net1    node2   4           1    1
5      net2    node1   5           1    1
6      net2    node2   6           1    1
7      net2    node1   7           1    1
8      net2    node2   8           1    1我希望统计的结果是:统计每个节点最新的一次数据。
7      net2    node1  7           1    1
8      net2    node2  8           1    1
但是查询语句导致netmac随机,有可能是net1,或者net2,这样:
7      net1    node1  7           1    1
8      net2    node2  8           1    1

解决方案 »

  1.   

    完整的查询语句如下:
    select max(data.id) id,data.net_mac,data.node_mac, max(node.name) node_name, max(data.up_date) up_date, 
      group_concat(data.temperature order by up_date desc separator ",") as temperature, 
      group_concat(data.humidity order by up_date desc separator ",") as humidity,
      group_concat(data.pressure order by up_date desc separator ",") as pressure, 
      group_concat(data.illumination order by up_date desc separator ",") as illumination
      from t_data data left join t_node node on node.mac = data.node_mac 
      where 1=1
      group by node_mac 
      order by node_name ASC
      

  2.   

    select data.* from 
    (select nodemac,max(updatetime) updatetime from t_data group by nodemac)t
    left join t_data data 
    on data.nodemac=t.nodemac and data.updatetime=t.updatetime
      

  3.   

    非常感谢您的答复。怪我没有描述清楚,我需要查询最新的2次记录,用于描述:当前的记录时上升(标红,并加&nbsp;&uarr;</span>)还是下降
    (标绿色,并加&nbsp;&darr;</span>)
      

  4.   

    更正一下:我需要查询最新的2次记录,用于描述:当前的记录时上升(标红,并加&nbsp;&uarr;</span>)还是下降
    (标绿色,并加&nbsp;&darr;</span>)
      

  5.   

    每个nodemac 2次最新的updatetime?
    select data.* from 
    (select nodemac,updatetime,row_number()over(partition by nodemac order by updatetime) tid from t_data )t
    left join t_data data 
    on tid<=2 and data.nodemac=t.nodemac and data.updatetime=t.updatetime
      

  6.   

    mysql是不是不支持?
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(partition by node_mac order by up_date) tid from t_data) t
    left join t_data dat' at line 2 select data.* from 
    (select node_mac,up_date,row_number()over(partition by node_mac order by up_date) tid from t_data) t
    left join t_data data 
    on tid<=2 and data.node_mac=t.node_mac and data.up_date=t.up_date
      

  7.   

    select data.* from 
    (select nodemac,updatetime,row_number()over(partition by nodemac order by updatetime) tid from t_data )t
    left join t_data data 
    on data.nodemac=t.nodemac and data.updatetime=t.updatetime
    where tid<=2
    上面tid<=2 应该放在where 中
      

  8.   

    mysql 要用变量 但也是可以实现 思路是差不多的
      

  9.   

    大侠,帮忙指点一下。实不相瞒,这个问题苦恼了好几个月了。
    我现在的实现方法是:先查出每个数据的最新一系列值,然后用json显示最新的一个值。如下面这一句:
      group_concat(data.temperature order by up_date desc separator ",") as temperature, 
      

  10.   

    mysql 现在没环境,我试着写个 你测试下select * from 
      (
      select nodemac,updatetime,
        (select count(*) from t_data b   where b. nodemac=a.nodemac and b.updatetime<=a.updatetime) row 
      from  t_data a
      )
    where row<=2
      

  11.   

    select d.* from 
      (select * from 
        (
        select nodemac,updatetime,
          (select count(*) from t_data b   where b. nodemac=a.nodemac and b.updatetime<=a.updatetime) row 
        from  t_data a
        )c
      where row<=2
      )t
    left join
     t_data d 
    on d.nodemac=t.nodemac and d.updatetime=t.updatetime
      

  12.   

    另外,在我二楼的查询方法基础上,可否解决我的问题:net_mac字段不要取错
      

  13.   

    嗯 把上面的 SQL语句中nodemac 都改成netmac
    (netmac,updatetime,)要建组合索引 
    另:你的结果行大约多少?
      

  14.   

    不清楚mysql是否支持 with as 语法 如果支持
    ;with t as 
    (select pid,MAX(id)id from a group by pid)
    select * from t
    union
    select a.pid,MAX(a.id) from a join t on a.pid=t.pid and a.id<t.id group by a.pid
    不支持就这样
    select pid,MAX(id)id from a group by pid
    union
    select a.pid,MAX(a.id) from a 
    join (select pid,MAX(id)id from a group by pid)t 
    on a.pid=t.pid and a.id<t.id group by a.pid
      

  15.   

    pid 换成 netmac
    id换成updatetime
    还有表名
      

  16.   

    Quote: 引用 16 楼 ayalicer 的回复:
    mysql 不支持 with as:刚才写一句sql有多个结果union的情况,union里面有一部分是子查询是各个union可以公用的,很想定义一个table然后各个各部分引用,因为担心数据库会重复执行相同的查询。百度了一下,看到SQL有个with...as...的语法,但郁闷的是mysql不支持,要写的话只能定义临时表了,坑爹的是其它数据库都支持。大家咱解决这个问题的?stackoverflow上说的方法好像不好搞啊。
    http://stackoverflow.com/questions/324935/mysql-with-clause
    mysql到底是不是数据库,我开始动摇了......