MySQL Join table using a reference - mysql

I need help with MySQL. I am trying to JOIN methods (Left and Inner now)
EDIT: I would also like to INSERT, can someone show me how? Should I use the Trigger? You may show the 2 possible solutions.
I have 4 Tables:
User, Project and User-Project, AssignedProject
Table User-Project has
ID | UserID | ProjectID
Table User has
ID | COMPANYID | UserName |
Table Project has
ID | ProjectName
Table AssignedProject
ID | COMPANYID | ProjectName
What I want to do.
I want to join User-Project table with data of User and Project using AssignedProject as my reference table. I don't know if this makes any sense... I will rephrase.
The association of project and user are in the table AssignedProject. However, I want to have the data in User-Project. Though I just need the ID (which are a foreign key in User-Project)
Example:
AssignedProject
1 | 1001 | AprojectName
Project
1| AprojectName
User
1 | 1001 | Mike
THEN
User-Project
1 | 1 | 1

Is this what you are looking for?
select u.UserName, p.ProjectName
from user_project up
inner join user u on u.id = up.user_id
inner join project p on p.id = up.project_id
For each record in user_project, the query retrieves the name of the associated user and project in tables user and project. This is how I understood your question. I cannot see how table AssignedProject relates to the other tables.

Related

Inserting a title when two IDs match, but excluding IDs from the query

I have two tables:
CREATE TABLE roles (
id INT PRIMARY KEY,
title VARCHAR(30)
);
CREATE TABLE employee (
id INT PRIMARY KEY,
first_name VARCHAR(30),
role_id INT
);
Where role_id corresponds to a role with a matching ID in the "roles" table. I am wanting to select my database in such a way that the end result would look like:
+----+------------+-------------------+
| id | first_name | role |
+----+------------+-------------------+
| 1 | Jane | Manager |
| 2 | Patrick | Project lead |
| 3 | Robert | Computer Engineer |
+----+------------+-------------------+
So that the user can view the employee data without the added clutter of employee.role_id and roles.id. I know I can use:
SELECT * FROM employee
RIGHT JOIN roles
ON role_id = roles.id;
To show all employees along with their corresponding roles, but in using this the roles.id and employee.role_id numbers are displayed along with the rest of the table, which is less than ideal for my case. I have toyed around with the idea of inserting the results into a third table, but I would rather not do this for simplicity's sake if possible.
How can I accomplish this?
I'm very new to MySQL and database management, and Googling around for the past day or two has revealed little in the way of a solution. (Or, more likely, a solution I was able to recognize as being the solution to my problem) I am using MySQL Server version 8.0.
If my understanding of your requirement is correct, you want select only some columns:
SELECT employee.id as id, first_name, title as role
FROM employee
RIGHT JOIN roles
ON role_id = roles.id;
This is a basic select join
select e.id,
e.first_name,
r.title
from roles r
inner join employee e on e.role_id=r.id;
https://dbfiddle.uk/ba-Hh-8P
Note. If there are employee without role change inner join to left join

MySQL Select Left Join multiple columns from the same table

I have objects in the Main Project Workflow table that I need to relate to one object in the User table. The issue I am facing is when I do a LEFT JOIN, I can only relate one object at a time.
The relation I need to do is:
workflowUID = user_id
assignedTo = user_id
I believe the problem is being caused by my LEFT JOIN, however, I don't know which join statement I need to use to do this relation.
User Table
user_id | user_firstName | user_lastName
1 | Joe | Smith
2 | John | Doe
Main Project Table
projectID | projectTitle | projectDesc | projectDueDate | projectAssignedTo
1 | Test Title | Desc for Proj | 11-06-2018 | 2
Main Project Workflow Table EDITED
projectID | CID | workflowUID | assignedTo
1 | 1 | 1 | 2
The projectID is releated to another table called mainProjects, which list more info about the project such as the project title, created/due date, created by, effort in hours, project description.
The CID is stored in the Main Project Workflow Table. It is the Commit ID. Which will be used later for stuff like editing/deleting comments.
Output
Workflow Created By | Workflow Assigned To
Joe Smith | John Doe
SQL:
SELECT *
FROM mainprojectworkflow
LEFT JOIN user ON mainprojectworkflow.workflowUID = user.user_id
WHERE projectID = $projectID
ORDER BY cid DESC
The second I try setting a second LEFT JOIN user ON mainprojectworkflow.workflowUID = user.user_id but instead, as a mainprojectworkflow.assignedTo I get a not unique table/alias user. I believe this is because I am already setting the user table to mainprojectworkflow.
EDIT
I'm sorry, I should have been more clear on what's going on.
END RESULT: My plan is to use the SQL to select the data and display it in PHP on a website. It's a project management website. I want to be able to have PHP pull the variables from SQL so I can use them however I feel fit.
You will need to join two times to the table user, like this:
SELECT
mpw.workflowUID,
CONCAT(cu.user_firstName, " ", cu.user_lastName) AS "Workflow Created By",
mpw.assginedTo,
CONCAT(au.user_firstName, " ", au.user_lastName) AS "Workflow Assigned To"
FROM
mainprojectworkflow AS mpw
INNER JOIN
user AS cu ON cu.user_id = mpw.workflowUID
INNER JOIN
user AS au ON au.user_id = mpw.assignedTo
WHERE
projectID = $projectID
ORDER BY
cid DESC
Try this type Of query :
SELECT
CONCAT_WS(' ', uc.user_firstName, uc.user_lastName) AS Workflow_Created_By,
CONCAT_WS(' ', ua.user_firstName, ua.user_lastName) AS Workflow_Assigned_To
FROM mainprojectworkflow
LEFT JOIN User uc ON mainprojectworkflow.workflowUID = uc.user_id
LEFT JOIN User ua ON mainprojectworkflow.assignedTo = ua.user_id;

MySQL Multiple Join with delimiting via FINDINSET

I am attempting to JOIN onto two different columns in the first table below from columns in the second and third tables.
I wish to JOIN users.id to job_listings.id to return users.username, and to also JOIN and delimit job_listings.categories to job_categories.id to return job_categories.description via FIND_IN_SET
job_listings
id | employer_id | categories
1 | 1 | 1,2
2 | 1 | 2
users
id | username | type
1 | foo | employer
2 | wat | employer
job_categories
id | description
1 | fun
2 | hak
I desire output that is of the following format:
output
username | type | category | description
foo | employer | 1 | fun
foo | employer | 2 | hak
foo | employer | 2 | hak
I have tried using various permutations of the following code:
SELECT users.username, users.type, job_listings.categories FROM users
JOIN job_listings ON users.id
JOIN job_listings AS category ON FIND_IN_SET(category.categories, job_categories.id)
ORDER BY users.username, category.categories
I know from other answers that I need to use an alias in order to use multiple JOIN operations with the same table, but despite adapting other answers I keep receiving errors related to declaring an alias, or returning output that has a column with the alias but no data returned in that column.
First, you should normalize your design. You should not store integer values in strings. You should not have foreign key references that you cannot declare as such. You should not store lists in strings. Is that enough reasons? You want a junction table for JobCategories with one row per job and one row per category.
Sometimes, we are stuck with other peoples lousy decisions and cannot readily change them. In that case, you want a query like:
SELECT u.username, u.type, jc.id, jc.category
FROM users u JOIN
job_listings jl
ON u.id = jl.employer_id and u.type = 'employer' join
job_categories jc
ON FIND_IN_SET(jc.id, j.categories) > 0
ORDER BY u.username, jc.category;
This query cannot take advantage of indexes for the category joins. That means that it will be slow. The proper data structure -- a junction table -- would fix this performance problem.

select in mysql joining two tables with two instance of second table column

Sorry I really can't construct a good title, but let me elaborate it. :(
I have table report and table user_account.
report table have columns:
user_id | reported_user_id | date
user_account table have columns:
user_id | name | email
I need to get the name of users who reported and name of the user who was reported in one column.
It's like :
user_id | name | reported_user_id | name | email | date
I've tried left join, but I only have this:
user_id | reported_user_id | name | email
This is my statement:
select user_account.name, user_account.email, report.*
from report
left join user_account where user_account.user_id = report.reported_user_id;
Please enlighten my mind. Thank you.
select reporter.name as reporter_name, reported.name as reported_name
from report
left join user_account reporter on report.user_id = reporter.user_id
left join user_account reported on report.reported_user_id = reported.user_id

SQL - can I do repeated inner joins again on the same column?

I have two simple tables, one called itineraries that holds details of holiday itineraries and one called users, that holds details of users. Other users create itineraries, and users can copy their itineraries and add travel agents, so the copied_from_id is the ID of the original creating user from users.id.
I've joined itineraries.user_id to users.id using the below query which works perfectly:
SELECT
itineraries.travel_agent_id,
itineraries.copied_from_id,
itineraries.user_id,
users.full_name,
users.username
FROM `gadabouting_gadabouting_production`.`itineraries`
INNER JOIN `gadabouting_gadabouting_production`.`users` ON itineraries.user_id=users.id
WHERE itineraries.travel_agent_id='253'
Giving me the following output:
+-----------------+------------------+---------+-------------+-------------+
| travel_agent_id | original_creator | user_id | full_name | username |
| 253 | 501 | 1465 | John Smithy | j.smithy |
| 253 | 501 | 1465 | John Smithy | j.smithy |
| 253 | 501 | 1474 | Ben Stockes | ben.stockes |
+-----------------+------------------+---------+-------------+-------------+
(The travel_agent_id and original_creator columns are the same as users.id).
What I want to do now is map the itineraries.travel_agent_id and itineraries.original creator to the users.full_name and users.username columns (so have the full_name and username columns printed next to each of the travel_agent_id and original_creator columns, but I just can't work out how to do it. I've spent hours on it now and can't get my head round it. Do I need to do more joins?
I've looked at several other SO questions about multiple joins but as far as I can see, none of them cover the process of 'going back' again and again on the same column as I want to do here.
Is this possible? Would greatly appreciate any help!
Thanks
You can join in the same table over and over, but you need to use an alias for each one so that you can specify which one you want to use. If you access the result by name, you also need alases for some of the field names.
(It's conventient to use aliases on other tables also, to make the query less verbose.)
select
i.travel_agent_id,
i.copied_from_id,
i.user_id,
u.full_name,
u.username,
ut.full_name as travel_agent_full_name,
ut.username as travel_agent_username,
uc.full_name as creator_full_name,
uc.username as creator_username
from
gadabouting_gadabouting_production.itineraries as i
inner join gadabouting_gadabouting_production.users as u on u.id = i.user_id
inner join gadabouting_gadabouting_production.users as ut on u.id = i.travel_agent_id
inner join gadabouting_gadabouting_production.users as uc on u.id = i.original_creator
where
i.travel_agent_id = '253'