Check two PHP tables

0

I have to make a list of the daily reports made by commercial agents and office managers in a single list. The reports are stored in the PPR table which has an id_user (if it is saved by an administrative) and a id_agent (if the agent makes the report). The user and agent tables each have a user_id and a file_id respectively. The issue is that I have no way for the query to go well because both the USER and the AGENT have the same "name" and "surname" fields ... I get the reports but without the name. Putting LEFT JOIN in both I get the full list of reports but only the names of the agents,

 <?php
    $query = mysql_query("select * from ppr
    LEFT JOIN users ON users.user_id = ppr.id_user
    LEFT JOIN agent ON agent.agent_id = ppr.id_agent
    ORDER BY id_ppr DESC") or die(mysql_error());
    ?> 

AGENTS table

Id_agente | nombre | apellidos

USERS Table

Id_user | nombre | apellidos

REPORTS Table (PPR)

Id_reporte | Id_user | Id_agente | reporte

Example:

nombre apellidos | tipo      | reporte
-----------------+-----------+-------
Jose Poveda      | (agente)  | Reporte del día 4 de marzo....
Vicente Pérez    | (usuario) | Reporte del día 4 de marzo...
    
asked by Vieira 14.03.2018 в 10:11
source

2 answers

2

It is common to find that the tables have the same names of columns, normally what has to be done is to identify the names of the columns separately using an alias. For example:

select a.nombre, b.nombre from tabla1 a, tabla2 b...

In the previous example, table1 and table2 have a column with the same name, but for each table an alias has been defined, a and b respectively, so that a particular field of one of the 2 tables can now be referenced by ex: a.name

You must use this same technique in your PHP query, you should also limit the number of fields to consult and do not use select * , specify the name of each column to be used in a timely manner.

Now, I understand you want to cross the 3 tables the query that you pose in essence goes well

select * from ppr 
LEFT JOIN users ON users.user_id = ppr.id_user 
LEFT JOIN agent ON agent.agent_id = ppr.id_agent 
ORDER BY id_ppr DESC

Applying the suggestion of the aliases we would have something like this:

select p.*, u.nombres, u.apellidos, a.nombres, a.apellidos from ppr p 
LEFT JOIN users u ON u.user_id = p.id_user 
LEFT JOIN agent a ON a.agent_id = p.id_agent 
ORDER BY p.id_ppr DESC
    
answered by 14.03.2018 / 10:29
source
0

Actually from my point of view you have a design error, agents and users should be a single table, since both are users but with different roles. As the tables are designed you are forced to have a polymorphic table (ppr) that is rarely the best or only solution, just as you have done it, the table ppr will have if or if null fields in all the records that you will have to deal with.

Having said that, we could avoid null records and opt for a cleaner result, using UNION on the same table, I give an example:

/* creamos las tablas */
CREATE TABLE IF NOT EXISTS users(Id integer PRIMARY KEY, nombre text, apellidos text);
CREATE TABLE IF NOT EXISTS agent(Id integer PRIMARY KEY, nombre text, apellidos text);
CREATE TABLE IF NOT EXISTS ppr(Id integer PRIMARY KEY, id_user integer, id_agente integer, reporte text);

/* creamos filas en users */
INSERT INTO users VALUES(1,'Tomy', 'u pepe');
INSERT INTO users VALUES(2,'lili', 'a Ruko');

/* mostramos users */
SELECT * FROM users;


/* creamos filas en agent */
INSERT INTO agent VALUES(1,'Tom', 'ape joae');
INSERT INTO agent VALUES(2,'Lucy', 'acc tio');

/* mostramos agent */
SELECT * FROM agent;


/* creamos filas en ppr */

INSERT INTO ppr (id, id_user, id_agente, reporte) VALUES(1, 1, null, 'reporte user 1');
INSERT INTO ppr (id, id_user, id_agente, reporte) VALUES(2, null, 1, 'reporte agente 1');
INSERT INTO ppr (id, id_user, id_agente, reporte) VALUES(3, 2, null, 'reporte user 2');
INSERT INTO ppr (id, id_user, id_agente, reporte) VALUES(4, null, 2, 'reporte agente 2');



/* consulta UNION */

SELECT ppr.id, ppr.id_agente as user, 'agente' as tipo, agent.nombre, agent.apellidos, ppr.reporte FROM ppr
INNER JOIN agent ON agent.id = ppr.id_agente
WHERE id_agente IS NOT NULL
UNION
SELECT ppr.id, ppr.id_user as user, 'usuario' as tipo, users.nombre, users.apellidos, ppr.reporte FROM ppr 
INNER JOIN users ON users.id = ppr.id_user
WHERE id_user IS NOT NULL;

Result:

id | users | tipo    | nombre | apellidos | reporte
--------------------------------------------------
2  | 1     | agente  | Tom    | joae      | reporte agente 1
4  | 2     | agente  | Lucy   | acc tio   | reporte agente 2
1  | 1     | usuario | Tomy   | u pepe    | reporte user 1
3  | 2     | usuario | lili   | a Ruko    | reporte user 2
    
answered by 14.03.2018 в 14:34