三表查询MySQL

时间:2011-01-13 22:42:52

标签: mysql

我有三个共享affiliate_id字段的表:

impressions, clicks, commissions

我想在一个查询中从这三个表中选择affiliate_id ='$ iAid'的所有数据,然后在php中处理数据。

我如何编写这样的查询?

6 个答案:

答案 0 :(得分: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."'";

答案 1 :(得分: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

答案 2 :(得分: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'

应该做的伎俩

答案 3 :(得分: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....

答案 4 :(得分: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 = ?

答案 5 :(得分: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'