根据另一个,group by,left join,AND,sum和where更新另一列

时间:2016-12-13 13:10:01

标签: php mysql cron

我有一个查询没有"到期日期少于今天"我试图通过给出代码,数据库结构和预期结果的例子来尽可能地解释

$query="UPDATE users c
LEFT JOIN (
  SELECT user_id, SUM(edexpf) as total
  FROM user_pages
  GROUP BY user_id
) x ON c.id = x.user_id 
SET c.edexpn = COALESCE(x.total,0);
";

以下是我使用db结构的示例结果

用户:

    id       edexpn
========== ===========
    1         0
    2         4

user_pages:

          expiry     user_id    edexpf         type
         ========== =========== ==========  ==========
2015-11-15 07:17:34      2          1         Static
2016-12-15 17:00:01      2          1         Dynamic
2016-12-15 17:00:01      2          1         New
2016-12-15 17:00:01      2          1         Dynamic
  

现在我期待这样的结果

用户:

     id       edexpn
========== ===========
     2          2
  

因为来自 user_pages:的页面之一,其中 user_id = 2 有1   通过日期页面和2个即将到期的到期日期页面并且类型不像静态,所以,最后我不想获得过期日期页面的总和,如果类型像静态和像新的那样

我认为我们可以添加类似的东西

        // Where CURDATE() < DATE(expiry)
    AND type NOT LIKE '%Static%'
AND type NOT LIKE '%New%'

但不确定如何

0 个答案:

没有答案
相关问题