dongyong5912 2013-10-26 09:17
浏览 103

查询内部查询,其中表2.amount(stat = 1)的总和大于表1费用的总和

I have two tables

table:transaction

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

table:campaign

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 --

I need campaign.id after filters using php and mysql

condition: if total money/transaction received from a user is greater than total expenses by a user. Money is also approved money (1=approved, 0=pending). Campaign is on means 1, who placed maximum bid

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

bid = max(bid)

Camps status = 1

1 step solution

 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)"));

not working

Alternatively if any solution in 3 steps -

1st step

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

2nd step

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

3rd step

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

Please help, I will sleep after solving it

  • 写回答

2条回答 默认 最新

  • dreamact3026 2013-10-26 10:11
    关注

    Try this:

    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)
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行