PostgreSQL:帮我弄清楚如何使用表别名

时间:2013-03-14 15:41:39

标签: postgresql join alias

请把剩余的头发保存在我的头上并帮我为这些语句制作别名,我在这里加入一张带有查找表的表格

//this does the location lookup
SELECT 
ludevice.device,
ludevice.description,
lulocation.location_long
FROM mopdb.ludevice
LEFT JOIN lulocation ON (ludevice.location_id = lulocation.location_id)
WHERE (ludevice.location_id = 1)
ORDER BY ludevice.device_id

编辑删除了令人困惑的评论
编辑所需的结果是一个用更少的字符实现此功能的语句

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT 
  d.device,
  d.description,
  l.location_long
FROM mopdb.ludevice d
LEFT JOIN lulocation l ON (d.location_id = l.location_id)
WHERE (d.location_id = 1)
ORDER BY d.device_id

或略短于此USING

SELECT 
  d.device,
  d.description,
  l.location_long
FROM mopdb.ludevice d
LEFT JOIN lulocation l USING (location_id)
WHERE (d.location_id = 1)
ORDER BY d.device_id