0

我有两张桌子

表:交易

id . user_id . amount . status
1 - - 100 -- --- 500 - ----- 1
2 - - 100 -- --- 100 - ----- 0
3 - - 110 -- --- 200 - ----- 1

表:活动

id . user_id . bid . status . budget . expense . size
1 -- 100 ---- 80  --- 1 ------- 200 ------ 200 ---- 5 --
2 -- 109 ---- 75  --- 1 ------- 050 ------ 030 ---- 2 --
3 -- 100 ---- 65  --- 1 ------- 700 ------ 065 ---- 2 --
4 -- 107 ---- 77  --- 0 ------- 020 ------ 020 ---- 2 --
5 -- 90 ----- 87  --- 1 ------- 120 ------ 090 ---- 7 --

我需要使用 php 和 mysql 过滤后的campaign.id

条件:如果从用户那里收到的总金额/交易大于用户的总支出。钱也是批准的钱(1=批准,0=待定)。广告系列在方式 1 上,谁提出了最高出价

(sum(transaction.amount) WHERE transaction.status = 1 of that any user) > sum(campaign.expense of that user)

出价 = 最高(出价)

营地状态 = 1

1步解决方案

 mysql_query("SELECT campaign.* FROM campaign c,transaction t  
            WHERE budget>expense AND status='1' AND size='2' 
            ON c.user_id=t.user_id 
            HAVING SUM (CASE WHEN transaction.status=1 THEN transaction.amount ELSE 0 END) > SUM(campaign.expense)"));

不工作

或者,如果有 3 个步骤中的任何解决方案 -

第一步

   mysql_query("SELECT id,user_id FROM campaign 
        WHERE budget>expense AND status='1' AND size='2'")

第二步

  keep id,user_id of 1st step WHERE SUM(transaction.amount status=1)
             > SUM(campaign.expense)

第三步

  mysql_query("SELECT id,user_id FROM campaign 
        WHERE step2 satisfied")

请帮忙,解决后我会睡觉

4

2 回答 2

0

据我了解,您希望实现以下目标:

SELECT 
    c.*
FROM campaign c
JOIN (SELECT
        SUM(amount) AS summed
    FROM transaction t
    WHERE status = 1
) temp t
WHERE c.status = 1
HAVING t.summed > SUM(c.expense)
ORDER BY c.bid DESC
LIMIT 1
于 2013-10-26T10:23:17.190 回答
0

尝试这个:

SELECT camps.id,camps.bid FROM camps c JOIN tran t  ON c.user_id=t.user_id 
    HAVING SUM (CASE WHEN tran.status=1 THEN tran.amount ELSE 0 END)
       > SUM(CASE WHEN camp.status= 1 THEN camps.expense ELSE 0 END)
于 2013-10-26T10:11:04.083 回答