1

我有这个查询,它从结果中给了我以前的 id,但它包含多个where子句。

SELECT DISTINCT Contacts.cm_id 
    FROM Contacts 
    LEFT JOIN Employee ON (Contacts.cm_id = Employee.ei_contact_id) AND (Employee.ei_primary_employer = "Y") 

WHERE ( CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) LIKE '%Recee Dawn%' 
    AND CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) NOT LIKE 'NULL' ) 

WHERE Contacts.cm_id < (
    SELECT DISTINCT Contacts.cm_id 
        FROM Contacts 
        LEFT JOIN Employee ON (Contacts.cm_id = Employee.ei_contact_id) AND (Employee.ei_primary_employer = "Y") 
        WHERE ( CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) LIKE '%Recee Dawn%' AND CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) NOT LIKE 'NULL' ) 
        WHERE Contacts.cm_id='77')

ORDER BY Contacts.cm_id DESC LIMIT 1

它给了我这个error

'WHERE Contacts.cm_id='313')) ORDER BY Contact_Master.cm_id DESC LIMIT 1' at line 10

如何调整此查询以避免查询中断。请帮忙

4

5 回答 5

1

您的查询语法错误,应该在caluseAND之后,并将caluses 与而不是每次结合起来,通常每个.LEFT JOIN ... ON ...WHEREWHEREANDWHEREWHERESELECT

尝试这样的事情:

SELECT DISTINCT Contacts.cm_id 
FROM Contacts LEFT JOIN Employee 
     ON (Contacts.cm_id = Employee.ei_contact_id) 
WHERE (Employee.ei_primary_employer = "Y") 
  AND ( CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) LIKE '%Recee Dawn%' 
  AND CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) NOT LIKE 'NULL' ) 
  AND Contacts.cm_id < 
     (SELECT DISTINCT Contacts.cm_id 
      FROM Contacts LEFT JOIN Employee 
           ON (Contacts.cm_id = Employee.ei_contact_id)  
      WHERE (Employee.ei_primary_employer = "Y") 
        AND (CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) LIKE '%Recee Dawn%' 
        AND CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) NOT LIKE 'NULL' ) 
        AND Contacts.cm_id='77') ORDER BY Contacts.cm_id DESC LIMIT 1)
于 2013-05-14T10:52:59.093 回答
0

格式化查询使其可读如果您在 where 子句中使用任何聚合函数,请使用

拥有

于 2013-05-14T10:52:44.550 回答
0

我认为您不应该有 2 个“WHERE”语句。在每个语句中将第二个“WHERE”更改为“AND”。

于 2013-05-14T10:55:52.203 回答
0

您不能编写多个 where 子句,除非它是另一个查询中的子查询。您可以根据需要通过放置“AND”或“OR”而不是其他 WHERE 来调整查询。

试试下面的查询(我放置的是 AND 而不是 WHERE,您可以根据需要调整 AND。)

SELECT DISTINCT Contacts.cm_id FROM Contacts 
LEFT JOIN Employee ON (Contacts.cm_id = Employee.ei_contact_id) AND 
(Employee.ei_primary_employer = "Y") 

WHERE ( CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) LIKE '%Recee Dawn%' AND 
CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) NOT LIKE 'NULL' ) 
AND Contacts.cm_id < (SELECT DISTINCT Contacts.cm_id FROM Contacts LEFT JOIN Employee  
ON (Contacts.cm_id = Employee.ei_contact_id) AND (Employee.ei_primary_employer = "Y") 
AND ( CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) LIKE '%Recee Dawn%' AND  
CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) NOT LIKE 'NULL' ) 
AND Contacts.cm_id='77') 
ORDER BY Contacts.cm_id DESC LIMIT 1

*注意:*此查询未经测试,仅供您参考。它可能/可能不起作用。

谢谢

于 2013-05-14T10:56:34.390 回答
0

我建议你再次检查MySQL 中 SELECT 的语法,因为你的查询完全错误。

由于我不知道您想要检索的数据库结构和原始任务,我不确定结果是否正确。

但是摆脱你的查询中的过度我得到了这个:

SELECT MAX Contacts.cm_id as cm_id FROM Contacts LEFT JOIN Employee ON (Contacts.cm_id = Employee.ei_contact_id) WHERE Employee.ei_primary_employer = "Y" AND CONCAT( Contacts.cm_fname," ", Contacts.cm_lname ) LIKE '%Recee Dawn%' AND Contacts.cm_id < 77 

希望你能得到你所期望的。

于 2013-05-14T11:16:13.957 回答