我有共同affiliate_id
場三個表:三個表查詢的MySQL
impressions, clicks, commissions
我要選擇這三個表中的所有數據,其中affiliate_id =「$ IAID」在一個查詢,然後處理數據在PHP中。
我該如何寫這樣的查詢?
我有共同affiliate_id
場三個表:三個表查詢的MySQL
impressions, clicks, commissions
我要選擇這三個表中的所有數據,其中affiliate_id =「$ IAID」在一個查詢,然後處理數據在PHP中。
我該如何寫這樣的查詢?
這裏的假設您使用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."'";
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
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'
應該做的伎倆
做左連接在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....
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 = ?
怎麼樣加入:
select * from impressions, clicks, commissions
where impressions.affiliate_id = clicks.affiliate_id
and clicks.affiliate_id = commissions.affiliate_id
and impressions.affiliate_id = '$iAid'
首先,您將學習基本的SQL以及如何聯接表。然後你會寫這個查詢。 – 2011-01-13 22:44:56