query to print the data from multiple tables - mysql

I have a list of tables as follows
tbluser
(userid, username)
tblmember
(memberid, fk_userid)
tblleader
(leaderid, fk_userid)
tbltask
(taskid, fk_memberid, fk_leaderid, taskname, taskstatus)
here the user will be a member and can be a leader too
i want to generate a query to show the list of the assigned task to the members and their leaders name.
Sl. No | Member Name | Task Name | Task Status | Leader Name
-------------------------------------------------------------------------
I am having joining issues as both member and leader table is referenced to the same table(user table).
Can anybody help me to get the query to show the data.

You question wasn't clear enough ,but i think your looking for this.
SELECT taskname,taskstatus,username
FROM tbltask,tblmember,tblleader,tbuser
WHERE (tblmember.memberid = tbltask.fk_memberid) AND (tbltask.fk_leaderid =
tblleader.leaderid) AND (tblleader.fk_userid = tbluser.userid);

SELECT
*
FROM
table1
INNER JOIN
tabel2 ON table2.id = table1.id
INNER JOIN
table3 ON table3.id = table2.id;

Sample data might have helped you better here, But I think you can try the below query -
SELECT userid AS "Sl. No",
username "Member Name",
taskname "Task Name",
taskstatus "Task Status",
Leader Name
FROM tbluser TU
JOIN tblmember TM ON TM.fk_userid = TU.userid
JOIN tblleader TL ON TL.fk_userid = TU.userid
JOIN tbltask TT ON TL.leaderid TT.fk_leaderid
AND TM.memberid = TT.fk_memberid;

Related

How to check user's permission based on 2 fields - mysql

Have 2 tables
User table with
user chat_id, kunnr (cust_code) & vkorg
Delivery table with
PO # and other delivery info, cust_code & sales_org
I want to verify if the user cust_code and sales_org match with shipmentdel's table
One user can belong to multiple sales org and a PO can have many delivery orders
Below is my working SQL statement, can this be simplified ?
SELECT * FROM ( SELECT a.* FROM ztelegram.ShipmentDel a
WHERE a.Cust_PO = 'WPO-01502') as t1
LEFT JOIN
( SELECT b.* FROM ztelegram.noti_setting b WHERE b.chat_id = '12345667' ) as t2
ON t1.sales_org = t2.vkorg AND t1.Sold_to_party_name = t2.kunnr
Without the need of t1 & t2, isnt the query working? Please check.
SELECT
*
FROM
ztelegram.ShipmentDel a
LEFT JOIN ztelegram.noti_setting b ON a.sales_org = b.vkorg AND a.Sold_to_party_name = b.kunnr
WHERE
a.Cust_PO = 'WPO-01502'
and b.chat_id = '12345667'

Multiple joins on same table

I'm trying to achieve a query which seems simple but I can't make it work correctly. Here's my database tables structures:
members
-> id
-> last_name
-> first_name
activities
-> id
registrations
-> id
-> member_id
tandems
-> id
-> activitie_id
-> registration_member_one
-> registration_member_two
Here's what i want to achieve:
Mutliple members can register to an activity. Then, i group the registrations by tandems. I want a view with all the tandems listed and there's my problem. When I try a query, it gives me multiple rows, duplicated many times.
Below, an example of the table I want to have:
tandems.id | activities.id | registration_member_one.members.last_name | registration_member_two.members.last_name
1 | 3 | John Doe | Jane Doe
Here's the query I'm working on:
SELECT
tandems.*,
memberOne.id, memberOne.last_name, memberOne.first_name,
memberTwo.id, memberTwo.last_name, memberTwo.first_name,
memberOne_registration.member_id as memberOne,
memberTwo_registration.member_id as memberTwo
FROM tandems
JOIN registrations as memberOne_registration
ON memberOne_registration.member_id = tandems.registration_member_one
JOIN members as memberOne ON memberOne.id = memberOne_registration.member_id
JOIN registrations as memberTwo_registration
ON memberTwo_registration.member_id = tandems.registration_member_two
JOIN members as memberTwo ON memberTwo.id = memberTwo_registration.member_id
WHERE activitie_id = 3;
Any help appreciated!
The error is caused by joining wrong column (member_id) of registrations table with tandems table, instead column registrations.id should be used.
SELECT
tandems.*,
memberOne.id, memberOne.last_name, memberOne.first_name,
memberTwo.id, memberTwo.last_name, memberTwo.first_name,
memberOne_registration.id as memberOne,
memberTwo_registration.id as memberTwo
FROM tandems
JOIN registrations as memberOne_registration ON memberOne_registration.id = tandems.registration_member_one
JOIN members as memberOne ON memberOne.id = memberOne_registration.member_id
JOIN registrations as memberTwo_registration ON memberTwo_registration.id = tandems.registration_member_two
JOIN members as memberTwo ON memberTwo.id = memberTwo_registration.member_id
WHERE activitie_id = 3;
Although other query is virtually the same, I hate working with unnecessarily long alias names so worked with "r1" and "r2" for the two instances of the registration table, and "m1" and "m2" for the members joining context.
SELECT
t.id,
t.activitie_id,
m1.last_name LastName1,
m1.first_name FirstName1,
m2.last_name LastName2,
m2.first_name FirstName2
FROM
tandems t
LEFT join registrations r1
ON t.registration_member_one = r1.id
LEFT JOIN members m1
ON r1.member_id = m1.id
LEFT join registrations r2
ON t.registration_member_two = m2.id
LEFT JOIN members m2
ON r2.member_id = m2.id
WHERE
t.activitie_id = 3;
To help you on this and in the future... Although mentally done, I try to mentally draw out how do I get the pieces together from the first table downstream. This can be seen too by the visual indentation almost like a tree view extension from T to R1 to M1, then R2 to M2 is a different branch. I also prefer to list the left table/alias.column = right table/alias.column in the join condition. How does T get to R1, then how does R1 get to M1.
In this, I used LEFT JOIN to each respective registration and member -- just-in-case only one person registered and a second may be pending. Not sure how your registration is actually structured.

Multiple joins on the same table

i have the following tables:
Technician
Tech_ID,First_Name,Last_Name
RT_QUEUE_Delta
Tech_ID, RT_Complete` (references a `Tech_ID` in `Technician`).
I need to get the data from a row in RT_Queue_Delta where RT_Completed = ?? but in my output I need to have the First_Name and Last_name that correlates with Tech_id and RT_Completed.
I can match one but I don't know how to match both. I tried:
select RTTech.First_Name as RT_First_Name,
RTTech.Last_Name as RT_Last_Name
from Technician as RTTech
Join RT_Queue_Delta as RT
on RT.RT_Completed = RTTech.Tech_ID
You can join to the Technician table multiple times:
select d.tech_id, t.first_name, t.last_name,
d.rt_completed as completed_id,
t2.first_name as completed_first_name,
t2.last_name as completed_last_name
from RT_QUEUE_Delta d
join Technician t on d.tech_id = t.tech_id
join Technician t2 on d.RT_Completed = t2.tech_id

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

Complex SQL query. At least for me

I am trying to pull invoice data from my database based on a PatientID. I am trying to figure out which invoices belong to which patient. Here is the important parts of my structure.
Invoices Table
InvoiceNumber | DateInvoice | DueDate | StudyID | TypeInvoice
Patients Table
FirstName | LastName | PatientID
InvoiceFields
id | InvoiceNumber | PatientID |
I need make a query that lists the invoice table data based upon a PatientID. Below is the query that I attempted, but got no where with. Thank you for your time.
SELECT Distinct
invoicefields.InvoiceNumber,
invoices.DateInvoice
FROM `invoices`, `patients`, `invoicefields`
WHERE invoicefields.PatientID = patients.PatientID
and invoicefields.InvoiceNumber = invoicefields.InvoiceNumber
GROUP BY invoicefields.InvoiceNumber
SELECT InvT.InvoiceNumber, InvT.DateInvoice
FROM InvoiceTable InvT
INNER JOIN InvoiceFields InvF ON InvF.InvoiceNumber = InvT.InvoiceNumber AND InvF.PatientID = #PatientID
So pretty much since you only need data from the InvoiceTable and you indicate you have the PatientID. I propose you just join to the Cross Reference table InvoiceFields and use the PatientID column in that query to filter it down to what you need. I had a more complex example using an exist before I realized you didn't need anything from Patients.
You could use this if you need information on the Patient as well (Just put the needed columns in the Select)
SELECT InvT.InvoiceNumber, InvT.DateInvoice
FROM InvoiceTable InvT
INNER JOIN InvoiceFields InvF ON InvF.InvoiceNumber = InvT.InvoiceNumber AND InvF.PatientID = #PatientID
INNER JOIN Patient Pat ON Pat.PatientID = InvF.PatientID
You can put the #PatientID portion on the join for either Patient or InvoiceFields. There really shouldn't be a performance difference between either way if you indexes are right.
The Response to the Below Comment but where I can show it cleaner:
SELECT IT.InvoiceNumber
,IT.DateInvoice
FROM InvoiceTable InvT
WHERE EXISTS (SELECT InvF.PatientID
FROM InvoiceFields InvF
WHERE InvF.InvoiceNumber = InvT.InvoiceNumber
AND InvF.PatientID = #PatientID)
This will return all the rows for the patient from InvoiceTable and if InvoiceNumber is Unique will not have any duplicates. Though this way you only have access to InvoiceTable to return Data from. If you only want one put a TOP 1 on it:
SELECT TOP 1 IT.InvoiceNumber
,IT.DateInvoice
FROM InvoiceTable InvT
WHERE EXISTS (SELECT InvF.PatientID
FROM InvoiceFields InvF
WHERE InvF.InvoiceNumber = InvT.InvoiceNumber
AND InvF.PatientID = #PatientID)
SELECT * from invoices i
inner join invoicefields iFld
on i.invoiceNumber = iFld.invoiceNumber
inner join patients p
on iFld.patientID = p.patientID
and p.patientID = 1235125
This should get you started in the right direction at least. I'm not sure what columns you wanted to return and/or if there's any nulls in the tables. Nulls will affect which rows are returned