我有这个查询,它从结果中提供了我之前的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
如何调整此查询以避免查询中断。 请帮忙
答案 0 :(得分:1)
您的查询语法错误,AND
之后的LEFT JOIN ... ON ...
应位于WHERE
caluse中,并将WHERE
calus与AND
组合而不是每次WHERE
,通常每个WHERE
只有一个SELECT
。
尝试这样的事情:
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)
答案 1 :(得分:0)
格式化查询使其可读 如果在where子句中使用任何聚合函数,请使用
HAVING
答案 2 :(得分:0)
我认为你不应该有2个'WHERE'语句。在每个陈述中将第二个'WHERE'更改为'AND'。
答案 3 :(得分: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
* 注意:* 此查询未经过测试,仅适用于您的想法。它可能/可能不起作用。
谢谢
答案 4 :(得分:0)
我建议您再次检查syntax of SELECT in MySQL,因为您的查询完全错误。
由于我不知道您想要检索的数据库结构和原始任务,我不确定结果是否正确。
但是在你的查询中摆脱了过度,我得到了这个:
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
希望你能得到你所期望的。