I have this code:
SELECT A.UNITCODE, B.FORMATIONCODE, C.UPPERFORMATIONCODE, D.UPPERFORMATIONCODE
FROM UNIT AS A.UNITCODE
INNER JOIN FORMATION AS B.FORMATIONCODE
INNER JOIN UPPERFORMATION_UNIT AS C.UPPERFORMATION
INNER JOIN UPPERFORMATION AS D.UPPERFORMATIONCODE
WHERE UNITCODE='7000007'
Can you guys help me how to join 4 tables with specified column?
Assuiming the all 3 related tables have the same UNIT_ID for join with table UNIT
SELECT
A.UNITCODE
, B.FORMATIONCODE
, C.UPPERFORMATIONCODE
, D.UPPERFORMATIONCODE
FROM UNIT AS A
INNER JOIN FORMATION AS B ON B.FORMATIONCODE = A.UNIT_ID
INNER JOIN UPPERFORMATION_UNIT AS C. C.UPPERFORMATION = A.UNIT_ID
INNER JOIN UPPERFORMATION AS D D.UPPERFORMATIONCODE = A.UNIT_ID
WHERE UNITCODE='7000007'
It seems you are confusing table aliases and linking columns.
This is how to give a table an alias name in the query in order to enhance readability:
INNER JOIN formation AS f
where the AS is optional. Most often it is ommited.
This is how to join:
FROM unit AS u
INNER JOIN upperformation_unit AS ufu ON ufu.unitcode = u.unitcode
Well, I don't know the columns linking the tables of course, let alone their names. But I suppose the query would have to look like this more or less:
SELECT
u.unitcode,
f.formationcode,
uf.upperformationcode,
ufu.upperformationcode
FROM unit u
JOIN upperformation_unit ufu ON ufu.unitcode = u.unitcode
JOIN upperformation uf ON uf.upperformationcode = ufu.upperformationcode
JOIN formation f ON f.formationcode uf.formationcode
WHERE u.unitcode = 7000007;
Related
I have some tables in my database, three main ones and one that holds the many-to-many relations.
1. Student (student_id, student_name)
2. Sport (sport_id, sport_name)
3. Departm (depart_id, depart_name)
4. Sch (sch_id, sch_name)
5. StudSport(relationid, studendid, sportid, departid, schid)
What I want to do is e.g. retrieve the name of the department based on the relations when I know the id. I can get the ids like this:
SELECT departid, schid from studsport
inner join Student on student_id = studentid
inner join Sport on sport_id = sportid
where student_id = 1 and sport_id=2
but I want to get the names of the department and the Sch from their corresponding tables, and I dont know how to do that.
As you don't select anything from Student or Sport, you can remove the corresponding inner joins.
SELECT d.depart_name, sch.sch_name FROM StudSport s
INNER JOIN Sch sch ON s.schid = sch.sch_id
INNER JOIN Departm d ON s.departid = d.depart_id
WHERE s.studendid = 1 AND s.sportid = 2
Something like this???
select sch.sch_nam, departm.depart_name,
-- what you have already --
Left outer Join StudSport on Student.student_id = Studsport.studentid and Sport.sport_id = StudSport.sportid
left outer Join Sch on StudSport.schid = Sch.sch_id
left outer join Departm on studsport.depart_id = studsport.departid
This is untested, a fiddle makes it much easier to give answers because of that.
EDIT - I misread your original query - before the downvotes start to rain - fixing it right now.
The way you should use LEFT OUTER and INNER joins is how the data is meant (again, a fiddle will normally be usefull) but it's just a couple of joins from what you have i guess:
select *
from studsport
join student on studsport.studentid = student.student_id
join sport on studsport.sportid = sport.sport_id
left outer Join Sch on StudSport.schid = Sch.sch_id
left outer join Departm on studsport.depart_id = studsport.departid
where student_id = 1 and sport_id=2
I have been working on the mimic II database and I don't understand how to use joins between 3 tables.
Currently I have:
SELECT p.hospital_expire_flg,
COUNT (*)
FROM poe_med m, poe_order o, d_patients p
WHERE m.poe_id=o.poe_id
AND o.subject_id=p.subject_id
AND drug_name_generic = 'Metoprolol'
GROUP BY p.hospital_expire_flg
I'm guessing I have to use JOIN AS for the 3 tables. But not sure how to.
You can do:
SELECT p.hospital_expire_flg
FROM poe_med `m`
INNER JOIN poe_order `o`
ON m.poe_id = o.poe_id
INNER JOIN d_patients p
ON o.subject_id = p.subject_id
WHERE drug_name_generic = 'Metroprolol';
A bit of a newbie question, probably an INNER JOIN with an "AS" statement, but I can't figure it out...
This is for a MYSQL based competition app. I want to select the "img_title" for both img_id1 and img_id2. I can't figure out how to do it and still see which title is assigned to the associated _id1 or _id2.
My tables:
competitions
comp_id
img_id1
img_id2
on_deck
img_id
img_title
Desired results:
comp_id | img_id1 | img_title1 |img_id2 | img_title2
You need a join for each image:
SELECT comp.comp_id, img1.img_id, img1.img_title, img2.img_id, img2.img_title
FROM competitions comp
INNER JOIN on_deck img1 ON img1.img_id = comp.img_id1
INNER JOIN on_deck img2 ON img2.img_id = comp.img_id2
LEFT JOIN if img_id1 or img_id2 can be NULL.
select comp_id, img_id1, b.img_title as img_title1,
img_id2, b2.img_title as img_title2
from competitions a
left outer join on_deck b on b.img_id = a.img_id1
left outer join on_deck b2 on b2.img_id = a.img_id2
switch let outer join to inner join if you want to exclude rows in competitions that do not have two matching img_ids
This query should give you the results you want. This also assumes that comp.img_id1 and comp.img_id2 are never NULL.
SELECT comp.comp_id
, comp.img_id1
, deck1.img_title AS img_title1
, comp.img_id2
, deck2.img_title AS img_title2
FROM competitions AS comp
JOIN on_deck deck1 ON comp.img_id1 = deck1.img_id
JOIN on_deck deck2 ON comp.img_id2 = deck2.img_id
If you have plan on having a NULL or empty string comp.img_id1 and/or comp.img_id2 fields, you'll need to do some left joins.
I hope I can explain myself.
I have a many to many table (asignaciones) which points to alumnos and invest tables. Both of those tables has a institucionID which points to instituciones table.
I need to get (in one query) both instituciones from alumnos and invest. I have this but is not complete. I guess if because of the AND in the last inner join:
SELECT
alumnos.alumnosID,
invest.investigadoresID,
asignaciones.alumnosID AS alumnosID1,
asignaciones.investigadoresID AS investigadoresID1,
instituciones.institucion
FROM alumnos
INNER JOIN asignaciones ON alumnos.alumnosID = asignaciones.alumnosID
INNER JOIN invest ON asignaciones.investigadoresID = invest.investigadoresID
INNER JOIN instituciones ON alumnos.institucionesID = instituciones.institucionesID AND invest.institucionesID = instituciones.institucionesID
This lacks the second institucion. I am getting just one
Any hints on this is really appreciated
This Query schold solve your problem:
SELECT
alumnos.alumnosID,
invest.investigadoresID,
asignaciones.alumnosID AS alumnosID1,
asignaciones.investigadoresID AS investigadoresID1,
instituciones.institucion
instituciones1.institucion
FROM alumnos
INNER JOIN asignaciones ON alumnos.alumnosID = asignaciones.alumnosID
INNER JOIN invest ON asignaciones.investigadoresID = invest.investigadoresID
INNER JOIN instituciones instituciones ON alumnos.institucionesID = instituciones.institucionesID
INNER JOIN instituciones instituciones1 ON invest.institucionesID = instituciones1.institucionesID
You have to join the last table twice.
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.