0

我有这个 SQL 查询:

SELECT * FROM magnitudes t1 INNER JOIN value_magnitudes t2 ON t1.id = t2.magnitude_id WHERE t1.floor_id = 42 AND t2.reading_date = (SELECT Max(reading_date) FROM value_magnitudes WHERE t2.magnitude_id = t1.id);

我的数据库架构是:

CREATE TABLE magnitudes (
  id INTEGER UNSIGNED  NOT NULL   AUTO_INCREMENT,
  name VARCHAR(255)  NULL  ,
  sdi_id VARCHAR(255)  NULL  ,
  sdi_id_floor VARCHAR(255)  NULL  ,
  visible TINYINT(1) UNSIGNED  NULL  ,
  history TINYINT(1) UNSIGNED  NULL  ,
  created_at DATETIME  NULL  ,
  updated_at DATETIME  NULL  ,
  floor_id INT(11)  NULL  ,
  unit VARCHAR(255)  NULL  ,
  kind_id INT(11)  NULL  ,
  sample_time INT(11)  NULL  ,
  history_from DATETIME  NULL DEFAULT 2011-02-22 11:18:07   ,
PRIMARY KEY(id));

CREATE TABLE value_magnitudes (
  id INTEGER UNSIGNED  NOT NULL   AUTO_INCREMENT,
  value FLOAT  NULL  ,
  magnitude_id INT(11)  NULL  ,
  sdi_belongs_id VARCHAR(255)  NULL  ,
  reading_date DATETIME  NULL  ,
  created_at DATETIME  NULL  ,
  updated_at DATETIME  NULL    ,
PRIMARY KEY(id));

我真正想要的是,得到最后一个取决于value_magnitudes最后一个updated_atmagnitudes.id on values_magnitudes.magnitude_id

谢谢。

4

2 回答 2

1
SELECT TOP 1 VM.* FROM value_magnitudes VM, magnitudes M  WHERE M.id = VM.id ORDER BY updated_at DESC
于 2013-04-24T12:09:18.880 回答
1

SELECT VM.* FROM value_magnitudes VM, magnitudes M WHERE M.id = VM.id ORDER BY updated_at DESC LIMIT 0,1;

于 2013-04-24T12:10:58.840 回答