I have two tables:
users
user_id INT(11) PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(45) NOT NULL,
user_pass VARCHAR(255) NOT NULL,
user_mail VARCHAR(100) NOT NULL,
user_dpto VARCHAR(100) NOT NULL,
user_resp BOOLEAN NOT NULL,
token VARCHAR(100)
solicitudes
sol_id INT(11) PRIMARY KEY AUTO_INCREMENT,
random_number INT(11) NOT NULL,
asunto VARCHAR(45) NOT NULL,
emision_date DATETIME NOT NULL,
state VARCHAR(45) NOT NULL,
state_date DATETIME NOT NULL,
tercero BOOLEAN NOT NULL,
nombre_tercero VARCHAR(100),
fecha_solicitud_tercero DATE,
fecha_limite DATE
And users_solicitudes
user_id INT(11),
sol_id INT(11),
rol BOOLEAN,
PRIMARY KEY (user_id, sol_id)
In this last table, rol is 0 if the person is who made the request, or 1 if is the person who took it.
Solicitudes (requests) are requests made to an specific department (support department in this case). I am making a dashboard where the admin can see all the "solicitudes", where it shows the person who made the request and the person who took that request (the last one can be more than one person).
The problem is that I can't find a way to make a query to show, in the same row, the person who made the request and the person who took it (simplest case). At the end, the table must be something like this:
user_who_made_request|asunto|user_who_took_it|state|state_date
I have looked other answers but can't get the result I want. Maybe is simpler than I think. I would appreciate any help.
If only one user per request can make that request, there is no need to store him in an extra table. Keep the extra table for the users who take the request though.
I'd eliminate your problem in setting up your tables that way:
CREATE TABLE solicitudes (
sol_id INT(11) PRIMARY KEY AUTO_INCREMENT,
user_id INT(11) NOT NULL, // the user who makes that request
random_number INT(11) NOT NULL,
asunto VARCHAR(45) NOT NULL,
emision_date DATETIME NOT NULL,
state VARCHAR(45) NOT NULL,
state_date DATETIME NOT NULL,
tercero BOOLEAN NOT NULL,
nombre_tercero VARCHAR(100),
fecha_solicitud_tercero DATE,
fecha_limite DATE
);
CREATE TABLE user_solicitudes(
user_id INT(11),
sol_id INT(11)
// rol BOOLEAN - no need for the role field anymore, as you'd only store those who took the request here.
);
try this way:
CREATE TABLE users (
user_id INT(11) PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(45) NOT NULL,
user_pass VARCHAR(255) NOT NULL,
user_mail VARCHAR(100) NOT NULL,
user_dpto VARCHAR(100) NOT NULL,
user_resp BOOLEAN NOT NULL,
token VARCHAR(100)
);
CREATE TABLE solicitudes (
sol_id INT(11) PRIMARY KEY AUTO_INCREMENT,
random_number INT(11) NOT NULL,
asunto VARCHAR(45) NOT NULL,
emision_date DATETIME NOT NULL,
state VARCHAR(45) NOT NULL,
state_date DATETIME NOT NULL,
tercero BOOLEAN NOT NULL,
nombre_tercero VARCHAR(100),
fecha_solicitud_tercero DATE,
fecha_limite DATE
);
CREATE TABLE user_solicitudes(
us_id INT AUTO_INCREMENT PRIMARY KEY,
req_user_id INT(11),
took_user_id INT(11),
sol_id INT(11),
rol BOOLEAN
);