【问题标题】:MySQL multiple inner joins, unions and where clauseMySQL 多个内连接、联合和 where 子句
【发布时间】:2014-05-30 19:17:23
【问题描述】:

编辑我原来的帖子。使用 LEFT 连接后,查询如下所示。

SELECT SQL_CALC_FOUND_ROWS fname, lname, desig, company, region, state, country, add_uid, contacts.`id` as id
        FROM contacts

         LEFT JOIN contact_art_collections ON contact_art_collections.contact_id = contacts.id
         AND (
             contact_art_collections.name LIKE '%singapore%'
             OR contact_art_collections.email LIKE '%singapore%'
             OR contact_art_collections.ph1_no LIKE '%singapore%'
             OR contact_art_collections.ph1_desc LIKE '%singapore%'
             OR contact_art_collections.ph2_no LIKE '%singapore%'
             OR contact_art_collections.ph2_desc LIKE '%singapore%'
             OR contact_art_collections.web LIKE '%singapore%'
             OR contact_art_collections.address LIKE '%singapore%'
         )

 LEFT JOIN contact_offices ON contact_offices.contact_id = contacts.id
 AND(
    contact_offices.off_address LIKE '%singapore%'
    OR contact_offices.off_phone1 LIKE '%singapore%'
    OR contact_offices.off_phone1_desc LIKE '%singapore%'
    OR contact_offices.off_phone2 LIKE '%singapore%'
    OR contact_offices.off_phone2_desc LIKE '%singapore%'
    OR contact_offices.off_phone3 LIKE '%singapore%'
    OR contact_offices.off_phone3_desc LIKE '%singapore%'
    OR contact_offices.off_city LIKE '%singapore%'
    OR contact_offices.off_zip LIKE '%singapore%'
    OR contact_offices.off_state LIKE '%singapore%'
    OR contact_offices.off_region LIKE '%singapore%'
    OR contact_offices.off_country LIKE '%singapore%'
 )

 LEFT JOIN contact_professional_details ON contact_professional_details.contact_id = contacts.id
 AND(
    contact_professional_details.pd_desig LIKE '%singapore%'
    OR contact_professional_details.pd_comp LIKE '%singapore%'
    OR contact_professional_details.pd_regd_comp LIKE '%singapore%'
 )

 LEFT JOIN contact_address ON contact_address.contact_id = contacts.id
 AND(
    contact_address.hmadd LIKE '%singapore%'
    OR contact_address.hmcity LIKE '%singapore%'
    OR contact_address.hmzip LIKE '%singapore%'
    OR contact_address.hmstate LIKE '%singapore%'
    OR contact_address.hmregion LIKE '%singapore%'
    OR contact_address.hmcountry LIKE '%singapore%'
 )

 LEFT JOIN contact_emails ON contact_emails.contact_id = contacts.id
 AND(
     contact_emails.email LIKE '%singapore%'
 )

 LEFT JOIN assistant_emails ON assistant_emails.contact_id = contacts.id
 AND(
     assistant_emails.email LIKE '%singapore%'
 )

 LEFT JOIN contact_fax ON contact_fax.contact_id = contacts.id
 AND(
    contact_fax.fax LIKE '%singapore%'
 )

 LEFT JOIN contact_phones ON contact_phones.contact_id = contacts.id
 AND(
     contact_phones.ph_no LIKE '%singapore%'
     OR contact_phones.ph_desc LIKE '%singapore%' 
 )

 LEFT JOIN assistant_phones ON assistant_phones.contact_id = contacts.id
 AND(
     assistant_phones.ph_no LIKE '%singapore%' 
     OR assistant_phones.ph_desc LIKE '%singapore%' 
 )

 LEFT JOIN contact_websites ON contact_websites.contact_id = contacts.id
 AND(
     contact_websites.web LIKE '%singapore%'
 )

        WHERE ( contacts.`title` LIKE '%singapore%' OR 
contacts.`fname` LIKE '%singapore%' OR 
contacts.`lname` LIKE '%singapore%' OR 
contacts.`full_name` LIKE '%singapore%' OR 
contacts.`desig` LIKE '%singapore%' OR 
contacts.`company` LIKE '%singapore%' OR 
contacts.`regd_company` LIKE '%singapore%' OR 
contacts.`remarks` LIKE '%singapore%' OR 
contacts.`artstage_contact` LIKE '%singapore%' OR 
contacts.`status` LIKE '%singapore%' OR 
contacts.`referred_by_name` LIKE '%singapore%' OR 
contacts.`vip_tier` LIKE '%singapore%' OR 
contacts.`vip_coll_tier` LIKE '%singapore%' OR 
contacts.`vip_influencer` LIKE '%singapore%' OR 
contacts.`vip_seniority` LIKE '%singapore%' OR 
contacts.`vip_as_fname` LIKE '%singapore%' OR 
contacts.`vip_as_lname` LIKE '%singapore%' OR 
contacts.`vip_as_email` LIKE '%singapore%' OR 
contacts.`vip_as_ph` LIKE '%singapore%' OR 
contacts.`vip_class_art_coll` LIKE '%singapore%' OR 
contacts.`vip_med_art_coll` LIKE '%singapore%' OR 
contacts.`vip_geo_int` LIKE '%singapore%' OR 
contacts.`media_art_media` LIKE '%singapore%' OR 
contacts.`media_freq` LIKE '%singapore%' OR 
contacts.`exb_waitlist` LIKE '%singapore%' OR 
contacts.`exb_blacklist` LIKE '%singapore%' OR 
contacts.`exb_greylist` LIKE '%singapore%' OR 
contacts.`exb_grade` LIKE '%singapore%' OR 
contacts.`exb_art_fairs` LIKE '%singapore%' OR 
contacts.`exb_exhibitor_applicant` LIKE '%singapore%' ) 

        ORDER BY  fname
                    asc
        LIMIT 0, 50

需要 42 秒来执行,这是很多时间。此处使用的所有 LIKE 字段都已创建索引。接下来将尝试使用临时表。

【问题讨论】:

  • 如果您删除所有 WHERE 子句,您会看到什么吗?如果是,请开始逐块添加它们,看看哪里出错了。如果不是,则意味着无法在公共字段上连接所有表。
  • 除了@Ashalynd 的建议,尝试将inner join 更改为left join 看看是否有任何结果。
  • 一个或多个表与原始 CONTACTS 表不匹配:由于您使用的是 INNER JOIN ,这将跳过结果。考虑使用 LEFT JOIN 来跳过结果。
  • 为什么都在暗示我几分钟前已经回答的问题?
  • 如果以后这些表中有很多数据 - 那么在这么多列上使用 LIKE '%<searchterm>%' 这样的查询肯定不会表现良好......

标签: mysql sql join inner-join


【解决方案1】:

您使用的是INNER JOIN,这表明即使在应用 WHERE 过滤器之前,只有在所有表中都有行的 id 才会带来数据。

我会使用LEFT JOIN 看看。然后应用 WHERE 条件

【讨论】:

  • 外连接表的所有条件都必须进入ON子句,否则我们回到INNER JOIN
  • 这似乎是最可能的解释。任何一个表中缺少匹配项的 id 都将被过滤掉,即使另一个表中的另一行匹配。
猜你喜欢
  • 1970-01-01
  • 2018-07-24
  • 2018-06-19
  • 1970-01-01
  • 2011-12-02
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
相关资源
最近更新 更多