【问题标题】:Updating a Bulky mySQL query for active users为活动用户更新 Bulk mySQL 查询
【发布时间】:2016-11-15 18:32:50
【问题描述】:

我有一个长形式的 PHP 函数来循环我的所有用户并测试他们的活动状态。它很慢并且需要多次操作...这个 SQL 操作要快得多,但现在但我的计数彼此不匹配。

我需要更新这个 mySQL 查询,以完成 1 次添加和 1 次调整。这样它就可以模拟测试用户活动的确切操作/功能。

"SELECT DISTINCT last.user_id
FROM ss_usermeta as last
JOIN ss_usermeta as next on last.user_id = next.user_id
WHERE last.meta_key = 'last_trans_date' and last.meta_value <= CURDATE()
and next.meta_key = 'next_recurring_date' and next.meta_value + INTERVAL 9 day >= CURDATE()";
  • 检查每个用户的 meta_key 是否有 'll_customer_id' 并确保它不为空。
  • 将 next_recurring_date 的比较器从当前日期更改为当前日期 + 9 天。

【问题讨论】:

  • 不确定我自己是否正在取得进展......但我已经更新它来做 + 9 天的事情......应该将 9 天添加到 ' 的值next_recurring_date' ``` SELECT DISTINCT last.user_id FROM ss_usermeta as last JOIN ss_usermeta as next on last.user_id = next.user_id WHERE last.meta_key = 'last_trans_date' and last.meta_value = CURDATE() ```

标签: php mysql sql wordpress


【解决方案1】:

为此,您需要另一个联接:

SELECT DISTINCT last.user_id
FROM ss_usermeta as last
JOIN ss_usermeta as next
  on last.user_id = next.user_id
JOIN ss_usermeta as third
  on third.user_id = next.user_id
WHERE last.meta_key = 'last_trans_date'
  and last.meta_value <= CURDATE()
and next.meta_key = 'next_recurring_date'
  and next.meta_value + INTERVAL 9 day >= CURDATE()
and third.meta_key = 'll_customer_id'
  and third.meta_value is not null;

【讨论】:

  • 非常感谢,计数减少了 10,但这极大地提高了我函数的准确性。我将继续重新评估我如何处理用户的活动状态。
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
相关资源
最近更新 更多