inner join mySQL - mysql

i have 3 tables
login - user_id, username
project - project_id, project_name
task - task_id, project_id, task_giver, task_receiver, task_content
*where task_giver and task_receiver has the user_id from login table*
MY QUERY :
SELECT login.username, project.project_name, tasks.task_content, tasks.task_giver
FROM tasks
JOIN login ON login.user_id = tasks.task_receiver
JOIN project ON tasks.project_id = project.project_id
;
But I am not getting the task_givers name getting only id.
How can I get his name?

JOIN the login table one more time like so:
SELECT
receivers.username 'Task receiver',
givers.username 'Task giver',
p.project_name,
t.task_content
FROM tasks t
INNER JOIN login receivers ON receivers.user_id = t.task_receiver
INNER JOIN login givers ON givers.user_id = t.task_giver
INNER JOIN project p ON t.project_id = p.project_id
SQL Fiddle Demo with some sample data

You have to JOIN login table twice (to task_receiver and to task_giver)

Related

Don't show any record given a condition (SQL)

Good morning, I am trying to perform the following query in MySQL:
Show the name and surname of the consultants who have not participated in any "Juan Perez" project.
I'm using the following query:
SELECT consultant.name, consultant.surname FROM consultor
INNER JOIN participate ON participate.id_consultant = consultant.id
INNER JOIN project ON project.id = participate.id_project
INNER JOIN cliente ON client.id = project.id_client
WHERE client.name NOT IN("Juan Perez")
But when I execute the query, it only hides those that are directly related in the tables.
How could I hide the other records where the consultants appear so that they do not appear?
Thanks.
I believe it should work if on the conditions of the join to client you exclude the client name there that it will return you what you want.
SELECT consultant.name, consultant.surname
FROM consultor
INNER JOIN participate ON participate.id_consultant = consultant.id
INNER JOIN project ON project.id = participate.id_project
INNER JOIN client ON client.id = project.id_client and client.name NOT IN ("Juan Perez");
I managed to solve it in the following way. Thanks to all for the help.
SELECT consultant.name, consultant.surname FROM consultant
LEFT JOIN (
SELECT id_project, participate.id_consultant FROM participate
INNER JOIN project
USING(id_project)
INNER JOIN client ON client.id = project.id_cliente
WHERE client.name like 'Juan Perez'
)
project ON project.id_consultant = consultant.id
WHERE project.id_project IS NULL;

How to select all records from a table by replacing values for two fields from other tables?

This is the table design of an application I need to build. Kindly ignore the Role Table in the image.
Is there any method by which I can select all the records in the table transfer log , but instead of showing "User No" and "Plant No" in the result , I need to show the corresponding Employee No/Username from the user table and corresponding Plant Id from Plant Table.
Try this;)
select distinct
log.LogNo, u.EmployeeNo/Username, p.PlantId, log.StartDate, log.EndDate, log.Active
from TransferLog log
left join UserTable u on u.UserNo = log.UserNo
left join PlantTable p on p.PlantNo = log.PlantNo
Use inner join
select a.*, b.username, c.pantname
from transfer_log as a
inner join User as b a.userno = b.userno
inner join plant as c a.plantno = c.plantno

Use inner join, left join or right join for join 2 tables

I'm realizing a project which raises me the next tables:
For being more specific: First table:
'docs' => doc_id, doc_type_type_id, clients_cli_id
where doc_type_type_id
invoices
reference guides
Second Table:
'client' => cli_id
What I try to do is to join Client with doc that My query is:
Show Client with his invoice and reference guide:
SELECT c.cli_name, d1.doc_file as f1 , d2.doc_file as f2 FROM clients c INNER JOIN docs d1 ON d1.client_cli_id = c.cli_id INNER JOIN docs d2 ON d2.client_cli_id = c.cli_id WHERE d1.doc_fec=d2.doc_fec
select * from docs
inner join client on docs.clients_cli_id = client.cli_id
where doc_type_type_id = 1
Something in this format should give you all invoices joined to client.

SQL join multiple times?

I am making a user status list of the following format "A like B's XXX". A and B are both registered users and have firstname and lastname and user id. How to join the status table with the user table twice to get the names of the two users? Thank you.
SELECT "SQACTION"."TIMECREATED",
"SQWORDLIST".*,
"SUBJECT"."FIRSTNAME" subject_fn,
"SUBJECT"."LASTNAME" subject_ln,
author.firstname author_fn,
author.lastname author_ln
FROM "SQACTION"
INNER JOIN "SQWORDLIST"
ON SQACTION.ACTION = SQWORDLIST.GUID
INNER JOIN "SQUSER" SUBJECT
ON SQACTION.SUBJECT = SUBJECT.GUID
LEFT JOIN SQDOCUMENT
ON SQACTION.ENTITY = SQDOCUMENT.GUID
LEFT JOIN SQUSER AUTHOR
ON SQDOCUMENT.AUTHORID = AUTHOR.GUID
WHERE (SUBJECT.GUID = 'B4D3BF632C0C4DB3AB01C8B284069D8F')
OR (SUBJECT.GUID IN ('67882AF3FA3C4254AF9A12CA0B0AB6E4',
'6A4B52FE233444838AACFE2AFFE4D38F',
'8CA3FB9061FF4710B51F1E398D3D1917'))
ORDER BY "TIMECREATED" DESC
This is what I have tried. Thank you.
You need to include the table name twice in the FROM clause, and use an alias so you can specify which fields from each instance of the table are used in the ON statement. You didn't provide enough details in your question to give an exact example, so here is something more general.
UserTable, with ID & Name
RegTable, with UserID, and SponsorID
select ut1.name as [User],
ut2.name as [Sponsor]
from UserTable ut1
inner join RegTable rt on ut1.id = rt.userid
inner join UserTable ut2 on rt.sponsorid = ut2.id
do you mean something like, status have two field links to user table?
select user_a.first_name as user_a_first_name, user_b.first_name as user_b_first_name, status.status_name
from status
left join users as user_a on user_a.id = status.user_from_id
left join users as user_b on user_b.id = status.user_to_id

How can I pull multiple user info from one query?

I have an invoice table that holds the id of the user that sent the invoice (from_id) and the user that received the invoice (to_id).
I want to be able to pull both of their info from the profile table but I am unable to figure out how.
Below is the query I'm running that let's me pull info for just one user (from_id) because of the join.
SELECT jobs.title, profiles.display_name, invoice.to_id, invoice.from_id, invoice.amount
FROM (invoice)
JOIN jobs ON jobs.job_id = invoice.job_id
JOIN profiles ON invoice.from_id = profiles.user_id
WHERE `invoice_id` = '3'
You can use the same table twice. Give them different aliased, which I think makes a query more readable anyway.
SELECT
j.title,
tp.display_name as to_name, fp.display_name as from_name,
i.to_id, i.from_id,
i.amount
FROM
invoice i
JOIN jobs j ON j.job_id = i.job_id
JOIN profiles fp ON i.from_id = fp.user_id
JOIN profiles tp ON i.to_id = tp.user_id
WHERE
i.invoice_id= '3'
You can join on a table more than once - in this case you need to join on the profiles table twice - once to get information about who the invoice is from and once to get the information about the user the invoice was sent to.
SELECT jobs.title
, Profile_From.display_name AS [From]
, Profile_To.display_name AS [To]
, invoice.to_id
, invoice.from_id
, invoice.amount
FROM invoice
JOIN jobs
ON jobs.job_id = invoice.job_id
JOIN profiles Profile_From
ON invoice.from_id = Profile_From.user_id
-- You are just missing this part
JOIN profiles Profile_To
ON invoice.to_id = Profile_To.user_id
WHERE invoice_id= '3'