当数据不在其中之一时,PHP 连接三个表

发布于 2024-12-06 10:02:14 字数 457 浏览 1 评论 0原文

我正在努力将三个表连接在一起,但在其中一个表中不会有任何信息,除非用户打开我们发送给他们的电子邮件

我当前正在使用此 SQL,但它似乎无法正常工作

SELECT email.senton, customer.*, count(tracker.viewed) as viewed 
FROM email_sent AS email, customer_detail AS customer, email_tracker AS tracker 
WHERE email.customer_id = customer.customer_id 
    AND customer.Email = tracker.emailaddress 
    AND customer.LeadOwnerId = '{$this->userid}'

这是由于该表email_tracker 可能没有客户信息,除非客户已打开电子邮件

I am working on joining three tables together, but in one of the tables no information will be in there, unless the user opens the email we sent them

I am currently using this sql but it seems not to work correctly

SELECT email.senton, customer.*, count(tracker.viewed) as viewed 
FROM email_sent AS email, customer_detail AS customer, email_tracker AS tracker 
WHERE email.customer_id = customer.customer_id 
    AND customer.Email = tracker.emailaddress 
    AND customer.LeadOwnerId = '{$this->userid}'

This is due to the table email_tracker may not have the customers info in it, unless the customer has opened the email

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

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

发布评论

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

评论(2

挽你眉间 2024-12-13 10:02:14

试试这个:

SELECT email.senton, customer.*, COUNT(tracker.viewed) as viewed 
FROM email_sent email INNER JOIN customer_detail customer
    ON email.customer_id = customer.customer_id
LEFT JOIN email_tracker tracker 
    ON customer.Email = tracker.emailaddress
WHERE customer.LeadOwnerId = '{$this->userid}'

LEFT JOIN子句用于始终获取左侧部分的列和右侧部分的列(如果它们存在于连接中)...

根据您的评论进行编辑:
尝试更改 COUNT(tracker.viewed) 部分,

CASE
    WHEN tracker.emailaddress IS NOT NULL THEN COUNT(tracker.viewed)
    ELSE 0
END
    as viewed 

我不确定它是否有效,我无法测试它,但请尝试一下

Try this:

SELECT email.senton, customer.*, COUNT(tracker.viewed) as viewed 
FROM email_sent email INNER JOIN customer_detail customer
    ON email.customer_id = customer.customer_id
LEFT JOIN email_tracker tracker 
    ON customer.Email = tracker.emailaddress
WHERE customer.LeadOwnerId = '{$this->userid}'

The LEFT JOIN clause is used to always get columns on the left part and columns on the right part if they exist in the join...

EDITED according to your comment:
Try to change COUNT(tracker.viewed) part with

CASE
    WHEN tracker.emailaddress IS NOT NULL THEN COUNT(tracker.viewed)
    ELSE 0
END
    as viewed 

I'm not sure it works, I cannot test it, but give it a try

满栀 2024-12-13 10:02:14

您想要的行为是LEFT OUTER JOIN(也称为LEFT JOIN)。如果其他表中不存在该行,它只是将值设置为 NULL。

SELECT email.senton, customer.*, COUNT(tracker.viewed) AS viewed
FROM email_sent AS email
    JOIN customer_detail AS customer USING(customer_id)
    LEFT OUTER JOIN email_tracker AS tracker ON customer.Email = tracker.emailaddress
WHERE customer.LeadOwnerId = '{$this->userid}'

PS:通常最好使用 JOIN 而不是笛卡尔积(您使用 , 所做的)。

The behavior that you want is the LEFT OUTER JOIN (also known as LEFT JOIN). It just set's the value to NULL if the row doesn't exist in the other table.

SELECT email.senton, customer.*, COUNT(tracker.viewed) AS viewed
FROM email_sent AS email
    JOIN customer_detail AS customer USING(customer_id)
    LEFT OUTER JOIN email_tracker AS tracker ON customer.Email = tracker.emailaddress
WHERE customer.LeadOwnerId = '{$this->userid}'

PS: It's generally a better idea to use JOINs instead of a Cartesian product (what you did with ,).

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