mysql数据库 有两个数据表 一个表名是sensors 代表传感器 有4列
 第一列control控制器编号 第二列no代表传感器的编号 由这两个数值确定唯一的传感器,第三列status  传感器的状态 第四列 memo 传感器的说明  
  另外一个表是sensordata,也是有四列  
前两列也是第一列control控制器编号 第二列no代表传感器的编号 由这两个数值确定唯一的传感器,第三列是datatime,代表一个时间 第四列是value,是一个值 该表数据是其他软件随时插入的 
  现在我想查询这个value值,条件是 最新时间的同一个传感器只要最新的那个值 现在我有一条语句 实现了只有第二个表的情况下查询的功能, 
select *from(select *from  sensordata order by datatime desc) as a group by control,no order by control;
现在我想关联第一个表  就是只查询第一个表中有的sensor(传感器),第二个表中的传感器比第一个多且第一个表中有的第二个表中肯定有,现在就是不想要第一个表中没有的传感器在第二个表中的数据 但是又不能在数据库中实际删除它 ,这个语句应该怎么写?、???

解决方案 »

  1.   

    贴建表及插入记录的SQL,及要求结果出来看看
      

  2.   

    create table sensors(
      control binary(16) not null,
      no smallint not null,
      status smallint not null default 0,
      memo varchar(255),
      primary key(control,no)
    );
    create table sensordata(
      datatime datetime not null,
      control binary(16) not null,
      no smallint not null,
      value    smallint not null
    )
    这是贴见表
    "insert into sensordata (datatime, control, no, value)" +" values(@datatime,@cno,@sno,@value)";
    insert into sensors(control,no, status, memo)" +" value(@ctrl,@no,@status,@memo)";
    要求 就是 先插入sensor表 根据该表生成了很多sensordata表中的数据,后来删除了表sensor中的一个传感器(及一条数据),但是第二个表中不删除数据,怎么把我那条查询语句关联到表sensor中 把那条属于被删除的那个传感器中的数据屏蔽掉 不知道我说明白没有 新手 很多都不懂
      

  3.   

    两个表中都是由control,no两个字段确定一个传感器  第二个表中代表该传感器的实时状态 
      

  4.   

    就是sensordata中有,sensors中没有的记录?
    如果是
    select * from sensordata a left join sensors b
    on a.control=b.control and a.no=b.no where b.no is null
      

  5.   


       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  6.   

    正好相反是两个表都有 的 而不是 sensor中没有的
      

  7.   

    select * from sensordata a inner join sensors b
    on a.control=b.control and a.no=b.no 
      

  8.   

     SELECT sd.control,sd.`no`,sd.`value` from sensors se INNER JOIN sensordata sd on se.control = sd.control and se.`no` = sd.`no` 
      where sd.datatime = 
    (SELECT MAX(sdd.datatime)  from sensordata sdd where sdd.control = sd.control and sdd.`no` = sd.`no`  )
    看这个
      

  9.   

    select * from (select * from sensordata a,sensor b where a.control=b.control and a.no=b.no ORDER BY datetime) c group by control,no
      

  10.   

    select * from (select a.control,a.no,a.datatime,a.value from sensordata a,sensor b where a.control=b.control and a.no=b.no ORDER BY datatime desc) c group by control,no; 
    可以实现我的要求 ;
    问题解决了  谢谢大家;