Multiple joins on the same table - mysql

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

Related

SQL Query help (possibly use JOIN?)

Table A [PATIENT] has columns [PATID], [FIRSTVISITDATE]
Table B [APPT] has columns [APPTID], [PATID], [CREATEDATE]
Table C [NOTE] has columns [NOTEID], [NOTETEXT]
Table D [PROCS] has column [PROCID], [PATID]
Table E [CHARGE] has columns [CHARGEID], [AMOUNT]
I need to sum CHARGE(AMOUNT) by PATID for all PATIENTS where the NOTE.NOTETEXT contains 'text' and one of the APPT for a PATIENT containing the 'text' has an APPT.CREATEDATE = to the PATIENT.FIRSTVISITDATE
Simply put I need to SUM the charges for PATIENTS if they have an appointment with 'text' in their notes and the appointment with that 'text' was their first visit to the office
Other key points:
CHARGE.CHARGEID = PROC.PROCID
NOTE.NOTEID = APPT.APPTID
With my limited knowledge of SQL I was able to sum for all patients regardless if the 'text' was included in their first appointments notes and for that I used:
select (SUM(AMOUNT)) as 'Cash Payments' from CHARGE where CHARGEID in
(select PROCID from PROC where PATID in
(select PATID from APPT where APPTID in
(select NOTEID from NOTE where NOTETEXT like '%text%')))
You can use the GROUP BY clause to group the AMOUNT by patient. You can filter your patients to just the ones with the text in the notes and FIRSTVISITDATE = CREATEDATE using an inner query that joins the tables on those conditions.
I have not tested the following query, but it should do what you're asking.
SELECT pa.PATIENT, SUM(c.AMOUNT) AS 'Cash Payments'
FROM PATIENT pa
INNER JOIN PROCS pr
ON pa.PATID = pr.PATID
INNER JOIN CHARGE c
ON pr.PROCID = c.CHARGEID
WHERE pa.PATIENT IN (
SELECT pa.PATIENT
FROM PATIENT pa
INNER JOIN APPT a
ON pa.PATID = a.PATID
AND pa.FIRSTVISITDATE = a.CREATEDATE
INNER JOIN NOTE n
ON a.APPTID = n.NOTEID
WHERE n.NOTETEXT LIKE '%text%'
)
GROUP BY pa.PATIENT;

joined query to return data

I have the following query which returns data only if the join exists. How do I return from the last joined table (#__unis) datas, even if there is no relationship between those tables without to write another query?
select * from #__unis_faculties AS faculty
join #__unis_subjects AS subject ON subject.faculty = faculty.id
join #__unis AS uni ON uni.id= subject.university
where uni.id = 1
table structure http://sqlfiddle.com/#!2/19add
use LEFT JOIN instead of join
select * from #__unis_faculties AS faculty
join #__unis_subjects AS subject ON subject.faculty = faculty.id
right join #__unis AS uni ON uni.id= subject.university
where uni.id = 1
Try this..
select * from #__unis_faculties AS faculty
join #__unis_subjects AS subject ON subject.faculty = faculty.id
left join #__unis AS uni ON ( uni.id= subject.university AND uni.id = 1 )

complex select query for mysql combining two column with where clause

i have two tables like
TABLE 1 : FACULTY_DETAILS
fac_det_id(pk)........fname...........availability
.....1......................... xxx.................full time
.....2......................... yyy.................part time
.....3......................... zzz.................weekdays
.....4......................... aaa.................partime
TABLE 2: FACULTY
faculty_id(pk)..........course_id........fac_det_id(fk)
.....1..............................1......................2
.....2..............................2......................3
.....3..............................3......................1
.....4..............................4......................3
.....5..............................3......................4
when i give course id
i need fname ,availability in table1 and faculty_id in table 2
ie if i give course_id=3 then i need like
faculty_id..........fname...........qualification
.....2...................xxx................full time
.....5...................aaa................partime
You can use this:
SELECT faculty_id, fname, availibility AS qualification
FROM faculty a, faculty_details b
WHERE a.fac_det_id = b.fac_det_id AND course_id = $your_course_id;
Try this:
SELECT f.faculty_id, fd.fname, fd.availability qualification
FROM FACULTY f
INNER JOIN FACULTY_DETAILS fd ON f.fac_det_id = fd.fac_det_id
WHERE f.course_id = 3
This should work
select f.faculty_id,fd.fname,fd.availability as qualification
from faculty f,faculty_details fd
inner join faculty_details fd ON f.fac_det_id = fd.fac_det_id
where course_id=3;

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

I need to finalize this MySQL multiple table JOIN

I have entires, equipments, brands, times and seasons.
entries:
id
time
equipment_1
equipment_2
equipments:
id
id_brand
brands:
id
name
times:
id
id_season
seasons:
id
name
My actual SQL query is:
SELECT entries.*, times.id_season AS id_season
FROM entries, seasons
WHERE entries.time = times.id
But in the final query I need the next information that I don't know how to obtain it:
The name for each entries.equipment_ as equipment_1_name and equipment_2_name which is set in brands.name.
The name of the season as season_name.
Thank you in advance!
Assuming you have normalized data. This avoid costly cartesian joins. I never use cartesian joins myself, although there are some cases where they are useful. Not here, though.
SELECT
entries.*,
times.id_seasons AS id_season,
b1.name AS equipment_1_name,
b2.name AS equipment_2_name,
seasons.name AS season_name
FROM entries
LEFT JOIN equipments AS equipments_1
ON equipments_1.id = entries.equipment_1
LEFT JOIN brands AS brands_1
ON brands_1.id = equipments_1.id_brand
LEFT JOIN equipments AS equipments_2
ON equipments_2.id = entries.equipment_2
LEFT JOIN brands AS brands_2
ON brands_2.id = equipments_2.id_brand
LEFT JOIN times
ON times.id = entries.time
LEFT JOIN seasons
ON seasons.id = times.id_season;