Search code examples
phpmysqlsqlleft-joinsql-like

MySQL query with LEFT JOIN and LIKE conditions doesn't show what I expected


I'm trying to make a query that return data from 4 tables, the problem comes when I try to put some aditional conditions. The query works with 4 tables with LEFT JOIN without the conditions but when I put the LIKE condition nothing change in the result. The result must be the select with "lat" that only begins with 40.95

This code it's working:

if($resultset=getSQLResultSet("SELECT name, lat, lng, description, 
COALESCE(section, 0), COALESCE(AVG(score), 0) FROM places P LEFT JOIN ratings R ON P.id=R.place LEFT JOIN ratings_sections RS ON R.id=RS.rating LEFT JOIN categories C ON P.type=C.type GROUP BY P.name, RS.section ORDER BY P.name, RS.section")){
while ($row = $resultset->fetch_array(MYSQLI_NUM)){
    echo json_encode($row);
    }
}

But nothing change when I put the LIKE condition:

if($resultset=getSQLResultSet("SELECT name,lat, lng, description, COALESCE(section, 0), COALESCE(AVG(score), 0) FROM places P LEFT JOIN ratings R ON P.id=R.place AND P.lat LIKE '40.95%' LEFT JOIN ratings_sections RS ON R.id=RS.rating LEFT JOIN categories C ON P.type=C.type GROUP BY P.name, RS.section ORDER BY P.name, RS.section")){
    while ($row = $resultset->fetch_array(MYSQLI_NUM)){
        echo json_encode($row);
    }
}

Result of the query


Solution

  • The SQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements.

    SELECT name,lat, lng, description,... FROM
    

    and

    SELECT score FROM
    

    Are different