我有两张桌子
表:交易
id . user_id . amount . status
1 - - 100 -- --- 500 - ----- 1
2 - - 100 -- --- 100 - ----- 0
3 - - 110 -- --- 200 - ----- 1
4 - - 100 -- --- 220 - ----- 1
5 - - 117 -- --- 120 - ----- 1
6 - - 122 -- --- 300 - ----- 1
这意味着用户存款但只有status=1有效
表:活动
id . user_id . bid . status . budget . expense . size. priority
1 -- 100 ---- 80 --- 0 ------- 200 ------ 200 --- 5 -- 1
2 -- 109 ---- 75 --- 1 ------- 050 ------ 030 --- 2 -- 2
3 -- 100 ---- 65 --- 1 ------- 700 ------ 065 --- 2 -- 4
4 -- 107 ---- 77 --- 0 ------- 020 ------ 020 --- 2 -- 3
5 -- 90 ----- 87 --- 1 ------- 120 ------ 090 --- 7 -- 7
我需要以下过滤后的活动ID(使用php和mysql)
条件:
01. campaign.status = 1
02. campaign.size = 2
03. campaign.budget > campaign.expense
04. those user_id has balance (balance = A - B)
A = sum(transaction.amount where transaction.status=1)
B = sum(campaign.expense)
05. order by bid max value
预期输出campaign.id = 3
for campaign.id 3 : status 1, size 2, budget 700 > expense 65
balance > 0 means for user_id 100 = A(500+220) > B(200+65) //means A-B = 455
我不是那么专家。尝试了几种方法
请帮忙,解决后我会睡觉的。输了2天。