新闻通讯队列的复杂 MySQL 查询

发布于 2024-07-21 00:19:38 字数 759 浏览 9 评论 0原文

大家好,我回来了,期待你们更多的精彩。 我有两个表:

  1. 新闻通讯 - 每行包含一个“id”、“主题”、“正文”和“新闻通讯”。 电子邮件
  2. newsletter_queue 的“from”标头 — 每行包含一个“id”、“email”地址、添加到队列中的“date”和“newsletterid”

我的目标是开发一个 MySQL 查询,可以从 ' 中提取 x 行数newsletter_queue”,然后按“newsletterid”对它们进行分组,同时使用 GROUP_CONCAT (或其他有效的方法)将所有电子邮件放入一个字符分隔的字符串中,我将用 PHP 解析该字符串。 我希望将它们放在一起的原因是因为我正在使用的邮件程序库(swiftmailer)接受包含用于批量电子邮件的电子邮件的数组。 另外,如果可能的话,将两个表连接在一起会非常整洁,从而避免第二次查询。

这是我到目前为止所得到的:

SELECT GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'), newsletterid, date
FROM newsletter_queue
WHERE status='0'
GROUP BY newsletterid
LIMIT 125

我的问题是 LIMIT 125 被应用于已经连接的行,由于我试图限制一次发送的电子邮件总数,而不是唯一的,所以它变得无用时事通讯。 如果有人能引导我走向正确的方向,我将非常感激。 如果您最终编写了这个示例,那也很好。

Hey everyone, I'm back and looking forward to more of your brilliance. I have two tables:

  1. newsletters — each row contains a 'id', 'subject', 'body' & 'from' headers for an email
  2. newsletter_queue — each row contains an 'id', 'email' address, 'date' added to queue and the 'newsletterid'

My goal is to develop a MySQL query that can pull x amount of rows from 'newsletter_queue', then group them by their 'newsletterid' while using GROUP_CONCAT (or whatever works) to put all the emails into a character separated string which I will parse with PHP. The reason I'd like to have them together is because the mailer library I am using (swiftmailer) accepts an array with emails for batch emails. Also, if possible, it would be very neat to join the two tables together, thereby avoiding a second query.

Here's what I have so far:

SELECT GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'), newsletterid, date
FROM newsletter_queue
WHERE status='0'
GROUP BY newsletterid
LIMIT 125

My problem is that the LIMIT 125 is being applied to the already concatenated rows, rendering it useless due to the fact that I'm trying to limit the amount of total emails being sent at a time, not unique newsletters. If anyone could guide me in the right direction, I would be very appreciative. If you wind up writing the example, that's great too.

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

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

发布评论

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

评论(3

萤火眠眠 2024-07-28 00:19:39
SELECT GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'), newsletterid, date
FROM 
   (SELECT email, newsletterid, date 
    FROM newsletter_queue
    WHERE status="0"
    ORDER BY date ASC
    LIMIT 125) as Unsent
GROUP BY newsletterid

这会在执行 group by 语句之前将限制应用于内部查询。 group by 语句位于外部查询中并不重要,因为 group by 无论如何都需要一个临时表并进行排序。 如果您需要对连接结果进行某种排序,则可以将其应用于外部查询,例如将 max 或 min 应用于日期并按其排序。

SELECT GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'), newsletterid, date
FROM 
   (SELECT email, newsletterid, date 
    FROM newsletter_queue
    WHERE status="0"
    ORDER BY date ASC
    LIMIT 125) as Unsent
GROUP BY newsletterid

This applies the limit to the inner query, before the group by statement is executed. It doesn't matter that the group by statement is in the outer query since a group by will need a temporary table and sort anyway. If you need some kind of ordering of the concatenated result, you can just apply it to the outer query, for instance by applying max or min to date and order by it.

滥情稳全场 2024-07-28 00:19:39

这也应该做你想做的事:

SELECT substring_index(GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'),'|',125), newsletterid, date
FROM newsletter_queue
WHERE status='0'
GROUP BY newsletterid

This should also do what you want:

SELECT substring_index(GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'),'|',125), newsletterid, date
FROM newsletter_queue
WHERE status='0'
GROUP BY newsletterid
空名 2024-07-28 00:19:39

疯狂的想法,但这行得通吗?

SELECT GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'), newsletterid, date
FROM newsletter_queue
WHERE status='0'
GROUP BY newsletterid, ID MOD 125

我怀疑一条记录中电子邮件地址的最大数量为 125。
遗憾的是,该金额并不精确为 125。它可能在 1 到 125 之间。

Wild idea but does this work?

SELECT GROUP_CONCAT(email ORDER BY date ASC SEPARATOR '|'), newsletterid, date
FROM newsletter_queue
WHERE status='0'
GROUP BY newsletterid, ID MOD 125

I'd suspect the maximum amount of email adresses in one record to be 125.
Unfortunately, the amount is not exact 125. It could be between 1 and 125.

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