SELECT with multiple tables returns only one project

Asked

Viewed 33 times

-2

I’m making a project management system, on the HOME page will appear all projects. In mysql I have in the PROJECTS table the client and project manager saved as User and Client ID... which has their data in other CLIENTS and USERS tabalas. I’m doing a SELECT in PHP in mysql that should return me all projects, but returns only one.

$sql = "SELECT projects.id P_ID, projects.gerente P_GERENTE, projects.cliente P_CLIENTE, projects.nome P_NOME, projects.area P_AREA, projects.data_final_plan P_DATA, users.nome U_NOME, users.sobrenome U_SOBRENOME, clients.nome C_NOME FROM projects INNER JOIN users ON projects.gerente = users.id INNER JOIN clients ON projects.cliente = clients.id";

Does anyone know why? I think by the Projects.manager = users.id and Projects.client = clients.id...

inserir a descrição da imagem aqui

I think because this ID=1 CLIENT=2 MANAGER=1 does not appear and ID=1 CLIENT=1 MANAGER=1 and this appears!! I need to make SELECT return without comparing if the manager ID matches the client ID!

  • How are the tables 'clients' and 'users'? You made their FK with the table 'Projects'?

1 answer

0


Try to trade the INNER for the LEFT

Ex:

$sql = "SELECT projects.id P_ID, 
           projects.gerente P_GERENTE, 
           projects.cliente P_CLIENTE, 
           projects.nome P_NOME,
           projects.area P_AREA, 
           projects.data_final_plan P_DATA, 
           users.nome U_NOME, 
           users.sobrenome U_SOBRENOME, 
           clients.nome C_NOME
      FROM projects 
      LEFT JOIN users   ON projects.gerente = users.id 
      LEFT JOIN clients ON projects.cliente = clients.id";
  • Perfect, solved my problem.

  • Follow the link with the difference between them. https://answall.com/questions/6441/qual-%C3%A9-a-difference%C3%A7a-entre-Inner-Join-e-outer-Join/118352 .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.