postgresql自我加入

时间:2011-04-13 11:11:32

标签: postgresql join

说我有一张这样的桌子

  id  |     device     |  cmd  | value | 
------+----------------+-------+---------

id = unique row ID
device = device identifier (mac address)
cmd = some arbitrary command
value = value of corresponding command

我想以某种方式自行加入此表,以获取特定设备的特定cmds及其对应值。

我不想只是SELECT cmd,value FROM table WHERE device='00:11:22:33:44:55';

假设我想要的值对应getnamegetlocation命令。我想输出像

这样的东西
        mac         |    name   | location
--------------------+-----------+------------
 00:11:22:33:44:55  | some name | somewhere

我的sql fu很漂亮。我一直在尝试不同的组合,比如SELECT a.value,b.value FROM table AS a INNER JOIN table AS b ON a.device=b.device,但我无处可去。

感谢您的帮助。

1 个答案:

答案 0 :(得分:8)

SELECT a.value AS thisval ,b.value AS thatval
FROM table AS a JOIN table AS b USING (device)
WHERE a.command='this' AND b.command='that';