MySQL 和 MySQL PHP:向属于最后 3 个活动线程的成员发送电子邮件

发布于 2024-10-18 04:54:16 字数 2003 浏览 6 评论 0原文

我下面的查询将从我的博客的通讯网络中获取最后 3 个活动线程,

SELECT ID, Approved, RecipientID, RecipientScreenname, RecipientEmail
FROM
(
SELECT 
root_strings.str_id as ID,
root_strings.str_approved as Approved,
root_strings.mem_id as RecipientID,

root_members_cfm.mem_screenname as RecipientScreenname,
root_members_cfm.mem_firstname as RecipientFirstname,
root_members_cfm.mem_email as RecipientEmail

FROM root_strings

LEFT JOIN root_members_cfm
ON root_members_cfm.mem_id = root_strings.mem_id

WHERE root_strings.parent_id = '1'
 AND root_strings.mem_id IS NOT NULL

UNION ALL

SELECT
root_strings.str_id as ID,
root_strings.str_approved as Approved,
root_strings.mem_id as RecipientID,

root_users.usr_screenname as RecipientScreenname,
root_users.usr_firstname as RecipientFirstname,
root_users.usr_email as RecipientEmail

FROM root_strings

LEFT JOIN root_users
ON root_users.usr_id = root_strings.usr_id

WHERE root_strings.parent_id = '1'
AND root_strings.usr_id IS NOT NULL

) SQ
ORDER BY ID DESC
LIMIT 0,3

它返回这样的结果,

ID     Approved RecipientID  RecipientScreenname   RecipientEmail       
14      1           3          x                   [email protected]
13      n/a        NULL        y                   [email protected]
13      n/a        NULL        y                   [email protected]

然后我将向每个线程发送一封电子邮件。

foreach($items_thread as $item_thread) 
{
   $sentmail = mail($item_thread['RecipientEmail'],$email_subject,$email_content,$email_headers);
}

但当你仔细观察时,逻辑并不正确,因为我将发送 y 两次电子邮件!

y 应该只会收到一封电子邮件。我该如何修复它 - 我应该修复 sql 查询还是 php 代码?

My query below will get the last 3 active threads from my blog's commnets,

SELECT ID, Approved, RecipientID, RecipientScreenname, RecipientEmail
FROM
(
SELECT 
root_strings.str_id as ID,
root_strings.str_approved as Approved,
root_strings.mem_id as RecipientID,

root_members_cfm.mem_screenname as RecipientScreenname,
root_members_cfm.mem_firstname as RecipientFirstname,
root_members_cfm.mem_email as RecipientEmail

FROM root_strings

LEFT JOIN root_members_cfm
ON root_members_cfm.mem_id = root_strings.mem_id

WHERE root_strings.parent_id = '1'
 AND root_strings.mem_id IS NOT NULL

UNION ALL

SELECT
root_strings.str_id as ID,
root_strings.str_approved as Approved,
root_strings.mem_id as RecipientID,

root_users.usr_screenname as RecipientScreenname,
root_users.usr_firstname as RecipientFirstname,
root_users.usr_email as RecipientEmail

FROM root_strings

LEFT JOIN root_users
ON root_users.usr_id = root_strings.usr_id

WHERE root_strings.parent_id = '1'
AND root_strings.usr_id IS NOT NULL

) SQ
ORDER BY ID DESC
LIMIT 0,3

It returns a result like this,

ID     Approved RecipientID  RecipientScreenname   RecipientEmail       
14      1           3          x                   [email protected]
13      n/a        NULL        y                   [email protected]
13      n/a        NULL        y                   [email protected]

Then I will an email to each of them.

foreach($items_thread as $item_thread) 
{
   $sentmail = mail($item_thread['RecipientEmail'],$email_subject,$email_content,$email_headers);
}

But the logic is not correct when you look closer as I will send y twice of the email!

y should just get one email. How can I fix it - should I fix the sql query or the php code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2024-10-25 04:54:16

问题在于 NULL!=NULLNULL=NULL 也不是),因此 DISTINCT 不认为第二个和第三个结果是相同的。如果您更改查询以使 RecipientID 显示为 0(例如 COALESCE(RecipientID,0) AS RID),问题就会消失。

The problem is that NULL!=NULL (nor is NULL=NULL), so DISTINCT does not consider the 2nd and 3rd results to be the same. If you change your query so that RecipientID shows as 0 (e.g COALESCE(RecipientID,0) AS RID) the problem will go away.

恰似旧人归 2024-10-25 04:54:16

我现在有一个解决方案,它使用 array_unique,这里是 删除重复的项目来自数组

I have a solution for this now which is using array_unique, here it is Remove duplicate items from an array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文