0

私は2つのSQLテーブルを持っていalarmMessageますcoordinates. alarmMessageテーブルには、テーブルへの外部キー フィールドがありcoordinatesます。alarmMessage座標に最新(最大)の gpsDate フィールドがあるテーブルから単一の行を取得したいと考えています。

アラームメッセージ:

CREATE TABLE alarmmessage
(
  alarmmessageid bigint NOT NULL,
  mtstrackid character varying(40),
  coordinatesid bigint,
  totaldistance double precision,
  iostatusid bigint,
  alarmstatus character varying(10),
  totalworkingduration integer,
  maintenanceremainingtime integer,
  isread boolean NOT NULL DEFAULT false,
  CONSTRAINT pk_alarmmessageid PRIMARY KEY (alarmmessageid ),
  CONSTRAINT fk_coordinatesid FOREIGN KEY (coordinatesid)
      REFERENCES coordinates (coordinatesid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_iostatus FOREIGN KEY (iostatusid)
      REFERENCES iostatus (iostatusid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_mtstrackid FOREIGN KEY (mtstrackid)
      REFERENCES vehicle (mtstrackid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

座標:

CREATE TABLE coordinates
(
  coordinatesid bigint NOT NULL,
  latitude double precision NOT NULL,
  longitude double precision NOT NULL,
  gpsvalid boolean,
  gpsdate timestamp without time zone,
  bearingangle double precision,
  speed double precision,
  mtstrackid character varying(40),
  altitude double precision,
  CONSTRAINT pk_coordinates PRIMARY KEY (coordinatesid )
)
4

1 に答える 1

0

次の SQL を使用できます。

SELECT alarmmessage.* FROM alarmmessage
INNER JOIN coordinates ON alarmmessage.coordinatesid = coordinates.coordinatesid
ORDER BY gpsdate DESC
LIMIT 1
于 2013-02-06T11:36:48.840 に答える