I have this query which gives me previous id from the result but it contains multiple where
clause .
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
its giving me this error
'WHERE Contacts.cm_id='313')) ORDER BY Contact_Master.cm_id DESC LIMIT 1' at line 10
How can I adjust this query to avoid query break. Please help
Your query syntax ist wrong, the AND
after the LEFT JOIN ... ON ...
should be inside the WHERE
caluse, and combine the WHERE
caluses with AND
instead of WHERE
each time, normally you have only one WHERE
for each SELECT
.
Try something like this:
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)
Format the query make it readable If you are using any aggregate function in where clause, use
HAVING
I think that you should not have 2 'WHERE' statements. Change the second 'WHERE' to an 'AND' in each of the statements.
You can't write multiple where clauses unless it is a sub query in another query. You can adjust your query by placing "AND" or "OR" instead of the other WHEREs according to your need.
Try below query (I am placing AND instead of WHERE, you can adjust AND according to your needs.)
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
* Note: * This query is not tested, and is just for your idea. It may / may not work.
Thank you
I suggest you check the syntax of SELECT in MySQL again, because your query is totally wrong.
Due to the fact I don't know the database structure and the origin task what you wanted to retrieve, I'm not sure that the result will be correct.
But getting rid of excessiveness in your query I got this:
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
Hope you'll get what you expect.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.