当数据不在其中之一时,PHP 连接三个表
我正在努力将三个表连接在一起,但在其中一个表中不会有任何信息,除非用户打开我们发送给他们的电子邮件
我当前正在使用此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
LEFT JOIN子句用于始终获取左侧部分的列和右侧部分的列(如果它们存在于连接中)...
根据您的评论进行编辑:
尝试更改
COUNT(tracker.viewed)
部分,我不确定它是否有效,我无法测试它,但请尝试一下
Try this:
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 withI'm not sure it works, I cannot test it, but give it a try
您想要的行为是
LEFT OUTER JOIN
(也称为LEFT JOIN
)。如果其他表中不存在该行,它只是将值设置为 NULL。PS:通常最好使用
JOIN
而不是笛卡尔积(您使用,
所做的)。The behavior that you want is the
LEFT OUTER JOIN
(also known asLEFT JOIN
). It just set's the value toNULL
if the row doesn't exist in the other table.PS: It's generally a better idea to use
JOIN
s instead of a Cartesian product (what you did with,
).