从3个表中选择计算

时间:2013-05-02 13:47:23

标签: php mysql

我尝试在MySQL中进行查询,从3个表中获取数据,并在2个表中进行计算。但我真的不知道该怎么做。

我在PHP代码中有这个来计算“每次点击的Avage成本”:

  1. 获取所有广告系列。

      SELECT * FROM campaigns;
    
  2. 获取广告系列的点击次数:

    select SUM(id) as IALT2 from aktivitet where annonce_id = '@@CAMPAIGN_ID' group by ip");
    
  3. 从所有行中获取总计

    SELECT SUM(price) as IALT from money where ad = '@@CAMPAIGN_ID' group by id
    
  4. 现在我可以计算出“每次点击费用”

     "IALT / IALT2" = CPC
    

    然后应该说:

    select * from campaign order by CPC desc limit 0,1
    

1 个答案:

答案 0 :(得分:0)

select * from campaigns C
order by
(
    (select SUM(price) from money where ad = C.CAMPAIGN_ID)
    /
    (select SUM(id) from aktivitet where annonce_id = C.CAMPAIGN_ID)
)desc limit 0,1
只是猜测,但我想你想要这个