MySQL Select Left Join multiple columns from the same table - mysql

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;

Related

Unable to write a proper query to join two tables and fetch what I need

I'm trying to fetch data from table1 which doesn't have a column with a specific value I include
Consider a social media site:
I have a "posts" table where I save all the posts by the user with their User id,
and I have the "follow" table where I save all the data like who's following who.
Now I'm trying to get all the data from the posts table where the user isn't following them
Example:
posts table
| u_id | Post |
|:---- |:----:|
| 1 |post1 |
| 2 |post2 |
| 1 |post3 |
| 3 |post4 |
follow table:
| u_id | following |
|:---- |:---------:|
| 2 | 1 |
| 1 | 3 |
Now the scenario: Let's say I'm the logged-in user with user id: 2, as per the requirement I should only see the posts of users that I'm not following, i.e., user #2 (which is me) and user #3 only.
So far the query I tried is:
$query = "SELECT posts.id, posts.u_id, posts.title, posts.date
FROM posts
LEFT JOIN follows
ON posts.u_id=follows.u_id
WHERE (follows.u_id != '$u_id')";
The $u_id is the logged-in user's id which is in the above scenario #2.
Now I m trying to get all the data from the posts table where the user isn't following them
SELECT p.id, p.u_id, p.title, p.date
FROM posts p LEFT JOIN
follows f
ON p.u_id = f.following AND
f.u_id = ?
WHERE u.u_id IS NULL;
LEFT JOIN is a fine approach. However, this is looking at what posts match following not u_id. Then you want to return the rows where there are no matches.
You ask to find posts of users which you are not following. Use NOT IN, NOT EXISTS, or LEFT JOIN:
SELECT *
FROM posts
WHERE posts.u_id NOT IN (SELECT following FROM follows WHERE u_id = 'me')
AND posts.u_id <> 'me' -- Remove this line if you want to see your posts.
;
I assume you wouldn't follow yourself. Just remove the noted line in the WHERE clause to see your posts.
This is similar to the LEFT JOIN approach.

MySQL Join table using a reference

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.

what should i change my sql statement so that i can arrive with this table?

Hello i am stuck with this problem i have tables
Complaints:
Complaint table
User:
User table
and i want to extract complaints of the users that are in the specific province
example: if i want to extract the complaints of the province of ILOCOS my table would look like this
complaint_id | user_id | complaint_title| complaint_category | complaint_desc
---------------------------------------------------------------------------------
17 | 2 | 2 | Accidents | qwe123
ive arrived with this sql statement :
SELECT DISTINCT complaint.complaint_id, complaint.user_id,
complaint.complaint_title,complaint.complaint_desc
FROM complaint LEFT JOIN
user
ON user.province = 'ILOCOS' LEFT JOIN
complaint_media
ON complaint.complaint_id = complaint_media.complaint_id
You need to join complaint table with user table on user_id column otherwise it will be a cartesian join.
select distinct complaint.complaint_id, -- check if you really need "DISTINCT"
complaint.user_id,
complaint.complaint_title,
complaint.complaint_desc,
. . .
from complaint
join user
on complaint.user_id = user.user_id -- here
and user.province = 'ILOCOS'
join complaint_media
on complaint.complaint_id = complaint_media.complaint_id

Mysql select between two table without limiting if record appear on the joined table

I have been trying to figure out how to select data related to one id between to tables without limit it to the joined table. I tried using UNION, Inner join, JOIN, but it limit me to show records that are only in both tables. By example:
Table 1 (users)
id | name | register
1 | John | 2014-03-01
2 | Kate | 2014-03-02
etc..
Table 2 (birthdays by example)
id | user | birthday
1 | 1 | 1989-09-09
Note that kate dont have a record on the birthdays table, if i do:
SELECT U.id, name, register, B.birthday FROM users as U INNER JOIN birthday as B ON B.user = U.id
it will only shows JOHN data, i would like to select all my users and if the record do not exist on the joined table, still be able to select all my users, sort of:
id | name | register | birthday
1 | John | 2014-03-01 | 1989-09-09
2 | kate | 2014-03-02 | null or ''
3
4
etc.
Sorry if its a stupid question but i dont find the light on this one. I would appreciate the help.
Regards
You need a LEFT OUTER JOIN instead of the plain JOIN (also known as INNER JOIN), like this:
SELECT U.id, name, register, B.birthday
FROM users as U
LEFT JOIN birthday as B
ON B.user = U.id
A LEFT JOIN between users and birthday tables will contain all records of the "left" table (users), even if the join-condition does not find any matching record in the "right" table (birthday).
This excellent article on The Code Project will help you a lot: Visual Representation of SQL Joins.
Summary of all JOIN types:
Note: Mysql does not support FULL OUTER JOIN but it can be emulated. Useful articles:
https://stackoverflow.com/a/4796911
http://www.sql-tutorial.ru/en/book_full_join_and_mysql.html
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Use left outer join instead of inner join..
SELECT U.id, name, register, B.birthday
FROM users as U left join birthday as B ON B.user = U.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'