postgres的多个表的最后记录?

时间:2014-01-23 21:28:06

标签: sql postgresql

我有两个表,“设备”和“校准”在“设备”中有一些值,有些在我需要的“校准”中,但我需要每个表的最后记录,我试图做类似的事情这样:

select concat_ws(',',sim, simfile,txtlaser,txtflow)
from ( select "chkSimulation" as sim, 
      "txtSimFile" as simfile, 
      "txtLaserPower" as txtlaser, "txtRegularFlow" as txtflow from 
     "Devices","Calibrations" order by "Calibrations"."ID" desc limit 1) res;

但这并不能保证我获得了Devices表的最后一条记录,只保证了Calibrations表的最后一条记录。

编辑:我这样做但是我得到的结果是每列中有两列,每列都有 每个表中的两个变量用逗号分隔,我如何将所有4个变为一起?

select (select concat_ws(',',sim, simfile)
from (select "chkSimulation" as sim, "txtSimFile" as simfile 
    from "Devices" order by "ID" desc limit 1) res), 

(select concat_ws(',',txtpower, flow)
from (select "txtLaserPower" as txtpower, "txtRegularFlow" as flow
    from "Calibrations" order by "ID" desc limit 1)restwo) 
from "Devices", "Calibrations" limit 1

1 个答案:

答案 0 :(得分:1)

SELECT
  CONCAT_WS(',', sim, simfile, txtlaser, txtregflow)
FROM
  (SELECT
    chkSimulation AS sim,
    txtSimFile AS simfile
  FROM
    Devices
  ORDER BY
    ID DESC
  LIMIT 1) resone,
  (SELECT
    txtLaserPower AS txtlaser,
    txtRegularFlow AS txtregflow
  FROM
    Calibrations
  ORDER BY
    ID DESC
  LIMIT 1) restwo