gpt4 book ai didi

php - 连接 3 个表 mysql php

转载 作者:行者123 更新时间:2023-11-29 08:38:39 26 4
gpt4 key购买 nike

我不确定术语,但我现在基本上有一个 2 个表左连接,如下所示:

Snippets Table
snippet_id PK
snippet_text

snippets_link_email_id Table
snippet_id * (Links to Snippets Table)
email_id * (Links to emails Table)

emails Table
email_id
version_no

许多其他各种数据

所以发生的事情是解析日志并将其插入到电子邮件表中,每条记录都包含有关日志文本、发现的错误以及有关用户的各种信息的详细信息。

我需要从每个日志中获取的主要信息是错误,每个唯一的错误都存储在片段表中。

由于可能有许多电子邮件存在相同的错误,我喜欢运行以下查询来查看有多少个独特的错误:

$total_exception_check = mysql_query("SELECT * FROM snippets");
$total_exceptions = mysql_num_rows($total_exception_check);

while ($row = mysql_fetch_array($total_exception_check))
{
$i = $row['snippet_id'];
//Need to find total occurrences of this error

$feedback_query = mysql_query("SELECT * FROM snippets LEFT JOIN snippets_link_email_id ON snippets.snippet_id = snippets_link_email_id.snippet_id WHERE snippets_link_email_id.snippet_id = $i AND snippet_date BETWEEN CURRENT_TIMESTAMP - INTERVAL '$num_days' HOUR AND CURRENT_TIMESTAMP");
}
$tot_snippets = mysql_num_rows($feedback_query);

所以我基本上循环遍历每个独特的错误,然后搜索 snippets_link_email_id 表以查看记录该错误的次数,即有多少已解析的电子邮件出现该错误。

tot_snippets 告诉我每个片段的总金额。所以这太棒了,我现在可以构建每个错误及其记录次数的表格。

但是,在电子邮件表中有很多信息,包括版本号,如果我想获得电子邮件表中 version_no 值 = 2000 的唯一错误总数,该怎么办

我如何编辑这段代码才能看到它?

SELECT *
FROM snippets
LEFT JOIN snippets_link_email_id
ON snippets.snippet_id = snippets_link_email_id.snippet_id
WHERE
snippets_link_email_id.snippet_id = $i
AND snippet_date BETWEEN
CURRENT_TIMESTAMP - INTERVAL '$num_days' HOUR
AND CURRENT_TIMESTAMP

我在其他类似的帖子中尝试过这个,但我猜我还差得很远:

 SELECT *
FROM snippets, emails
LEFT JOIN snippets_link_email_id
ON snippets.snippet_id = snippets_link_email_id.snippet_id
AND snippets_link_email_id.email_id ON emails.email_id
WHERE snippets_link_email_id.snippet_id = $i
AND snippet_date BETWEEN
CURRENT_TIMESTAMP - INTERVAL '$num_days' HOUR AND CURRENT_TIMESTAMP

最佳答案

正确加入电子邮件可以解决问题吗?

 SELECT *
FROM snippets
LEFT JOIN snippets_link_email_id
ON snippets.snippet_id = snippets_link_email_id.snippet_id
LEFT JOIN emails
ON snippets_link_email_id.email_id = emails.email_id
WHERE snippets_link_email_id.snippet_id = $i
AND snippet_date BETWEEN CURRENT_TIMESTAMP - INTERVAL '$num_days' HOUR AND CURRENT_TIMESTAMP");

此外,我注意到您的第一个查询没有使用时间戳。这将返回比您真正需要的更多的行(我猜)。您可以按时间戳过滤第一个查询,然后您的循环将大大减小:

SELECT * FROM snippets
WHERE snippet_date BETWEEN CURRENT_TIMESTAMP - INTERVAL '$num_days' HOUR AND CURRENT_TIMESTAMP");

关于php - 连接 3 个表 mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14475800/

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