gpt4 book ai didi

php - 左连接 2 个表时如何减少 MySQL 上的用户优先级字段?

转载 作者:可可西里 更新时间:2023-11-01 07:43:59 26 4
gpt4 key购买 nike

假设我有emailspriority 表:

优先表字段:

Fields: uid, priority

电子邮件表字段:

Fields: id, uid, content

每个用户都有很多电子邮件。在脚本中,我根据用户的优先级获取用户的电子邮件(通过加入 2 个表)。但是当我批量获取并且具有最高优先级的用户在队列中有 1000 封邮件时,问题就出现了。第二个用户的优先级为 99。我应该最多发送 2 封优先级最高的用户的电子邮件,因为在第一个用户的第三条记录中,他的优先级为 97,低于第二个用户。

如何解决这个问题?我在 PHP 中编程,所以如果解决方案在 PHP 中得到更好的解决,请告诉我如何解决。

这是主要查询:

select * from emails e left join by priority p on e.uid=p.uid order by priority DESC

编辑 1:
电子邮件表中的数据:

id     uid     email_content

1 321 some example data
2 434 some other example data
3 321 another from first user
4 321 again another from 321

优先级表中的数据如下:

id      uid      priority

1 321 100
2 434 99

uid 等于 321 的用户最初具有最高优先级,但不是他所有的电子邮件。发送第一封电子邮件时优先级应为 99,第二封电子邮件优先级应等于 98。现在不应发送第三封电子邮件,uid 为 434 的用户的电子邮件具有最高优先级。

因为我已经获取了 10 条记录,所以没有获取更新的优先级!

最佳答案

只需简单地获取一个用户。发送电子邮件,然后更新优先级。

<?php
$mysqli = new mysqli("127.0.0.1", "root","", "stackoverflow");

if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}


$query = "SELECT e.id, email_content,p.uid from emails e INNER JOIN priority p ON e.uid=p.uid ORDER BY priority DESC LIMIT 1";


$updateQuery = "UPDATE priority SET priority = priority - 1 WHERE uid = ?";

while (true) {
$result = $mysqli->query($query);
if ($result->num_rows == 0) {
break;
}
$row = $result->fetch_row();
// @TODO send the mail
// delete the email entry
$mysqli->query("DELETE FROM emails WHERE id = $row[0]");
// NOW update the PRIORITY
$mysqli->query("UPDATE priority SET priority = priority - 1 WHERE uid = $row[2]");
}

关于php - 左连接 2 个表时如何减少 MySQL 上的用户优先级字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35599375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com