Query to get all records regardless of whether or not they exist in another table

1

I'm running an app from a store where users can leave equipment to be arranged. So I did a php to look for those users who have already left some equipment by name:

        $sql = mysqli_query("$conexao,
        SELECT u.contribuinte, u.nome, u.tlm
        FROM utilizador u, entrada e
        WHERE u.contribuinte = e.contribuinte
        AND u.nome LIKE  '%".$utilizador."%'
        ORDER BY e.data_entrada DESC 
        "); 

However in the query above I get these results, but I also wanted to get the users regardless if they already left some equipment or not. In other words, I want all users to appear, with priority being given to those who have already left some equipment.

This is the first time I've seen such a case, but I think it's possible to do this query. Thanks in advance for the help:)

    
asked by anonymous 27.07.2016 / 17:57

1 answer

1

Use LEFT JOIN for this.

SELECT u.contribuinte, u.nome, u.tlm
FROM utilizador AS u
LEFT JOIN entrada AS e ON u.contribuinte = e.contribuinte
WHERE u.nome LIKE  '%".$utilizador."%'
ORDER BY e.data_entrada DESC 
    
27.07.2016 / 18:05