2016-09-20 73 views
0

我有兩個表「數據」和「列表」,我正在做一個左加入。兩個表都有一個ID列。問題是,我無法弄清楚如何使用連接來做AS,所以我稍後可以在我的PHP腳本中引用data.ID和listing.ID。這是我當前的查詢,從而導致兩列雙方打成ID將AS table.column添加到LEFT JOIN查詢

`SELECT * FROM `listings` LEFT JOIN `data` ON (`listings`.`REALTOR` = `data`.`ID`) WHERE `listings`.`ACTIVE` = '1' AND `listings`.`MODIFIED` < NOW() - INTERVAL 90 DAY ` 

回答

1
SELECT l.*, d.*, 
     l.id as listing_id, 
     d.id as data_id 
FROM listings l 
LEFT JOIN data d ON l.REALTOR = d.ID 
WHERE l.ACTIVE = '1' 
AND l.MODIFIED < NOW() - INTERVAL 90 DAY 
+0

謝謝。我就這麼接近!我的許多嘗試之一是 'id as l.listing_id' UGH –