1

我有三张桌子——商店、土豆和番茄(番茄桌子看起来像土豆桌子)。土豆表中的 id 正是商店的 id - 所以那家商店的土豆的价格。

+----+-------+-----------+
|         potato         |
+----+-------+-----------+
| id | price | date_time |
+----+-------+-----------+

+----+-----+-----+------+------+  
|             shop             |
+----+-----+-----+------+------+
| id | lat | lng | name | geom |
+----+-----+-----+------+------+

我想做的是选择距离某个位置最大 10 公里的所有商店,以及番茄和土豆的价格。

现在,我有这个查询,它可以选择商店,以及土豆价格

SELECT
    shop.lat,
    shop.lng,
    shop.id,
    potato.date_time AS potato_date,
    potato.price as potato_price 
FROM 
    shop,
    potato 
WHERE
    potato.id = shop.id AND 
    ST_DWithin(
        ST_GeomFromText('POINT(xx.xxxxxx yy.yyyyyy)',4326),
        shop.geom,
        10*1000,
        true
    );

但我也想知道那家店西红柿的价格。怎么可能做到?

4

1 回答 1

3

Use Left JOIN

SELECT 
   shop.lat
  , shop.lng
  , shop.id
  , potato.date_time AS potato_date
  , potato.price as potato_price 
  , tomato.date_time as tomato_date
  , tomato.price as tomato_price
FROM shop
LEFT JOIN potato on potato.ID = shop.ID
LEFT JOIN tomato  on tomato.ID =  shop.ID 
WHERE  ST_DWithin(ST_GeomFromText('POINT(xx.xxxxxx yy.yyyyyy)',4326), shop.geom,10*1000, true );

If each shop sells tomatoes and potatoes you can use inner join.

于 2013-10-01T21:22:04.123 回答