doucang6739 2015-08-31 06:10
浏览 49
已采纳

在两个不同的表-MySQL中更新两个不同列的查询

I am working on upwork type project and want to create an update query for bids. User has free bids (which he gets by default per month) and pro bids which he can buy for certain amount.
Now if he would apply on any job then I want to update -1 for job_quota (if it is not 0 already), then -1 from pro bids in ascending order.

My table structure (in user table):

users

job_quota -------->id

12---------------->53

Buy_bids

id---->no_bids--->amount_paid--->user_id 

1-----> 20------------>20---------------> 53

2-----> 20------------>20---------------> 53

3-----> 20------------>20---------------> 53

I have an idea like this - but I do not how to implement it into MySQL query:

$query=<<< SQL
update users set job_quota=job_quota-1 if ( job_quota !=0 )
else update buy_bids set no_bids =no_bids-1 where user_id=53
SQL;

Can anyone suggest me how can I achieve it, I am not looking for accurate solution but I need your help and suggestion what should I try to achieve this.

Many Thanks.

  • 写回答

3条回答 默认 最新

  • douchaqi3369 2015-08-31 06:51
    关注

    Something Like this?

    Update users ,buy_bids 
    set users.job_quota = case when users.job_quota !=0 then 
                                    users.job_quota-1
                          else users.job_quota end,
        buy_bids.no_bids =case when users.job_quota =0 then 
                                     buy_bids.no_bids-1
                         else buy_bids.no_bids end
    WHERE users.user_id=buy_bids.user_id        
    and users.user_id=53;
    

    This is not tested. Just a suggestion. I'm not sure about the else condition,so just added users.job_quota =0 . Change that accordingly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改