2011-01-13 143 views
0

我有共同affiliate_id場三個表:三個表查詢的MySQL

impressions, clicks, commissions 

我要選擇這三個表中的所有數據,其中affiliate_id =「$ IAID」在一個查詢,然後處理數據在PHP中。

我該如何寫這樣的查詢?

+4

首先,您將學習基本的SQL以及如何聯接表。然後你會寫這個查詢。 – 2011-01-13 22:44:56

回答

1

這裏的假設您使用MYSQL的PHP​​代碼:

$qry = "SELECT im.*, cl.*, co.* 
FROM `impressions` im, `clicks` cl, `comissions` co 
WHERE im.`affiliate_id`=cl.`affiliate_id` 
AND cl.`affiliate_id`=co.`affiliate_id` 
AND im.`affiliate_id`='".$iAid."'"; 
0
SELECT <YOUR COLUMN LIST> 
    FROM impressions a, clicks b, commissions c 
WHERE a.affiliate_id = '$iAID' 
    AND a.affiliate_id = b.affiliate_id 
    AND b.affiliate_id = c.affiliate_id 
0
SELECT * FROM impressions AS i JOIN clicks AS c ON i.affiliate_id = c.affiliate_id JOIN commissions AS m ON i.affiliate_id = m.affiliate_id WHERE i.affiliate_id = '$iAid' 

應該做的伎倆

0

做左連接在MySQL查詢:

Select * from Impressions imp 
Left join clicks c on c.affiliate_id = imp.affiliate_id 
Left join commissions com on com.affiliate_id = imp_affiliate_id 
Where *** 
limit *** 
ect.... 
0
select * from 
impression a 
join clicks b on (a.affiliate_id = b.affiliate_id) 
join commissions c on (a.affiliate_id = c.affiliate_id) 
where a.affiliate_id = ? 
0

怎麼樣加入:

select * from impressions, clicks, commissions 
where impressions.affiliate_id = clicks.affiliate_id 
    and clicks.affiliate_id = commissions.affiliate_id 
    and impressions.affiliate_id = '$iAid'