繁体   English   中英

加入3个表mysql php

[英]Join 3 tables mysql php

我不确定这个术语,但是我现在基本上像这样保留了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 (columns or collations)
FROM table1
LEFT JOIN table2
ON (some condition that associates rows in table1 with rows in table2)
LEFT JOIN table3
ON (some condition that associates rows in table1 or table2 with rows in table3)
WHERE (whatever you want to limit the result set to)

无论您要联接多少个表,都遵循相同的基本模式,但是如果使用大量表,则为它们加别名可能会使您的生活变得更轻松。

SELECT (columns or collations)
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON (some condition that associates rows in t1 with rows in t2)
LEFT JOIN table3 AS t3
ON (some condition that associates rows in t1 or t2 with rows in t3)
WHERE (whatever you want to limit the result set to)

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

 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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM