Here is the problem. I’m searching cruises, based on a search field entry. I only want to return cruises that haven’t sailed yet. I’m searching title, destination, code in the cruises table and ports in the itinerary table. I search at the moment, and get cruises that are in the past ?
$query_search_cruises = sprintf("SELECT id, title, code FROM cruises WHERE departs > CURDATE() AND title LIKE %s OR destination LIKE %s OR code LIKE %s OR EXISTS (SELECT * FROM itinerary WHERE port LIKE %s AND cruises.id = cruise_id )", GetSQLValueString("%" . $colname_search . "%", "text"), GetSQLValueString("%" . $colname_search . "%", "text"), GetSQLValueString("%" . $colname_search . "%", "text"), GetSQLValueString("%" . $colname_search . "%", "text"));
In MySQL,
ANDhas a higher precedence thanORso your query is effectively:and
(destination LIKE %s)will get all rows matching that condition regardless of thedepartscolumn.If you want different precedence you will have to group the terms explicitly, such as with: