Search code examples
phpmysqlmysql-error-1064

Single force index throwing error on joined table


I'm trying to use a left join to bridge two tables and force index an index that only exists on the joined table, but I get the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FORCE INDEX (l.sfdcId) WHERE l.sfdcId = '003A000001eR0HsIAK' ORDER BY a.activity' at line 3

Here's an output of the query being run (works fine if I remove the FORCE INDEX):

SELECT a.activityDate,a.primaryAttributeValue,a.attributeDescription,l.firstName,l.lastName,l.title,l.email
FROM activities AS a LEFT JOIN
     leads AS l 
     ON a.leadId = l.leadId FORCE INDEX (l.sfdcId) 
WHERE l.sfdcId = '003A000001eR0HsIAK'
ORDER BY a.activityDate DESC 

Any idea why this would fail?


Solution

  • The FORCE INDEX goes after the table definition:

    SELECT a.activityDate,a.primaryAttributeValue,a.attributeDescription,
           l.firstName,l.lastName,l.title,l .email
    FROM activities a LEFT JOIN
         leads l FORCE INDEX (sfdcId)
         ON a.leadId = l.leadId 
    WHERE l.sfdcId = '003A000001eR0HsIAK'
    ORDER BY a.activityDate DESC ;