我有一个查询,我想提取那些没有参加本周锦标赛的用户并向他们发送电子邮件提醒。我有以下查询,但它没有显示任何结果。它本应该给我所有11个用户,因为这个比赛是下周。
SELECT
T1.username,
T2.*
FROM
(SELECT
id,
lead_id,
form_id,
MAX(case when field_number = 1 then value end) username,
MAX(case when field_number = 7 then value end) email,
MAX(case when field_number = 6 then value end) tournament_name,
MAX(case when field_number = 3 then value end) primary_golfer,
MAX(case when field_number = 4 then value end) backup_golfer,
MAX(case when field_number = 5 then value end) date,
MAX(case when field_number = 8 then value end) tournament_id
FROM `wp_rg_lead_detail`
GROUP BY lead_id
HAVING tournament_id = '4867aac6-186a-4508-9c57-69a468e1167f')T1
LEFT JOIN
(SELECT user_login, user_email FROM wp_users)T2 ON T1.username = T2.user_login
答案 0 :(得分:1)
我认为你的逻辑开始是错误的。你可能需要从所有玩家开始,然后将他们与锦标赛联系起来。使用LEFT JOIN和WHERE子句的IS NULL可以找到没有锦标赛条目的用户。
SELECT DISTINCT wp_users.*
FROM wp_users
LEFT JOIN (SELECT lead_id
, MAX(case when field_number = 7 then value end) AS email
, MAX(case when field_number = 8 then value end) AS tournament_id
FROM wp_rg_lead_detail
GROUP BY lead_id
) AS t1
ON t1.email = wp_users.user_email
AND t1.tournament_id = '4867aac6-186a-4508-9c57-69a468e1167f'
WHERE t1.lead_id IS NULL;