2013-10-01 3 views
1

나는 가게, 감자 및 토마토 (토마토 테이블은 감자 테이블처럼 보입니다)라는 세 개의 테이블이 있습니다. 감자 테이블에있는 이드는 실제로 이드의 이드입니다. 그래서 그 가게에있는 감자의 가격입니다. 내가하고 싶은 무엇여러 테이블에서 데이터 가져 오기

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

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

는 토마토와 감자 가격과 함께 어떤 위치에서 최대 10km, 모든 상점을 선택합니다.

는 지금, 나는 감자 가격

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 
    ); 

와 함께 상점을 선택할 수 있습니다이 쿼리를 가지고 그러나 나는 또한 그 가게에서 토마토의 가격을 좀하고 싶습니다. 어떻게 할 수 있니? 각 상점은 내부 조인 사용할 수있는 토마토와 감자를 판매하는 경우

답변

3

(왼쪽)는

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); 

가입하세요.

관련 문제