为活跃用户更新庞大的mySQL查询

时间:2016-11-15 18:32:50

标签: php mysql sql wordpress

我有一个长形式的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天。

1 个答案:

答案 0 :(得分: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;