MySQL Join忽略具有空电子邮件列值的行

时间:2016-04-06 13:32:48

标签: mysql join

我试图在MySQL中完成多个表的连接。我设法编写了以下查询:

SELECT
    CONCAT(c.first_name, ' ', c.last_name) as name,
    c.title,
    c.department,
    CONCAT(c2.first_name, ' ', c2.last_name) as reports_to,
    c.phone_work,
    c.phone_mobile,
    e.email_address
FROM 
    email_addresses e,
    email_addr_bean_rel eb,
    contacts c
LEFT JOIN
    contacts c2 ON c.reports_to_id = c2.id
WHERE
    c.id = eb.bean_id and
    e.id = eb.email_address_id;

这是生成一个包含我需要的数据的表,唯一的问题是这不包括完整的联系人列表。当我对联系人进行计数时,有130个但是通过上述查询,结果会减少到86。

经过一些调查后,我发现这是由于没有电子邮件地址被忽略的联系人而且我不想要这个,但我不知道如果他们有一个空条目,如何阻止他们被忽略

email_addresses表包含电子邮件地址,但要email address查找contact,必须使用email_addr_bean_rel {contacts id $order = wc_create_order(); $custaddress=array( // array fields here ); $order->add_product( get_product( '12' ), 2 ); //(get_product with id and next is for quantity) $order->set_address( $custaddress, 'billing' ); $order->set_address( $custaddress, 'shipping' ); $order->add_coupon('Fresher','10','2'); // accepted param $couponcode, $couponamount,$coupon_tax $order->calculate_totals(); {1}}首先

2 个答案:

答案 0 :(得分:1)

SELECT CONCAT_WS(' ',c.first_name,c.last_name) name
     , c.title
     , c.department
     , CONCAT_WS(' ',c2.first_name,c2.last_name) reports_to
     , c.phone_work
     , c.phone_mobile
     , e.email_address
  FROM contacts c
  LEFT 
  JOIN email_addr_bean_rel eb
    ON eb.bean_id = c.id
  LEFT
  JOIN email_addresses e
    ON e.id = eb.email_address_id
  LEFT 
  JOIN contacts c2 
    ON c2.id = c.reports_to_id

答案 1 :(得分:1)

我认为你的问题是连接表的顺序。因此,例如,如果您要列出所有联系人,无论是否有电子邮件,联系人都应该是您的第一个表。所以,我会使用JaydipJ的查询,只做一个小修改:

SELECT
 CONCAT(c.first_name, ' ', c.last_name) as name,
 c.title,
 c.department,
 CONCAT(c2.first_name, ' ', c2.last_name) as reports_to,
 c.phone_work,
 c.phone_mobile,
 e.email_address
FROM 
 contacts c
 LEFT JOIN  email_addr_bean_rel eb ON c.id = eb.bean_id
 LEFT JOIN  email_addresses e ON e.id = eb.email_address_id
 LEFT JOIN  contacts c2 ON c.reports_to_id = c2.id