I'm writing a query to list the first and last names of all staff who have ever taught any of the same course as Kim (Staff_Fname) Cox (Staff_Lname) or have ever taught in the same room (Loc_id) as Kim Cox.
So far I have written this (and it's full of errors):
SELECT KimClasses.Course_Code, KimClasses.Course_Name, St2.Staff_Fname, St2.Staff_Lname
FROM
(SELECT St.Staff_id, CS.C_SE_id, C.Course_code, C.Course_name
FROM Staff St
JOIN CourseSection CS ON St.Staff_id = CS.Staff_ID
JOIN Location L ON CS.Loc_ID = L.Loc_id
JOIN Course C ON CS.Course_ID = C.Course_ID
WHERE St.Staff_Fname = "Kim"
AND St.Staff_Lname = "Cox") KimClasses
JOIN CourseSection CS2 ON KimClasses.Course_ID = CS2.Course_ID
AND NOT KimClasses.Staff_id = St2.Staff_id;
But I'm not sure on how I would include the part to list the staff that have taught in the same room.
Database schema: http://i.stack.imgur.com/dTGV5.jpg
This should get you started...
Select s.staff_fname,s.staff_lname
from staff s join course_section cs
on s.staff_if = cs.staff_id
where cs.course_id in (select distinct s1.course_id from staff s1 join course_section sc1 where s1.Staff_Fname = 'kim' and s1.Staff_Lname = "Cox" )
OR s.loc_id in (select distinct s2.loc_id from staff s2 join course_section sc2 where s2.Staff_Fname = 'kim' and s2.Staff_Lname = "Cox" )
Related
I building a custom SQL query of a product name with id_product, id_lang, id_manufacturer, category, meta_description, brand name and multiple feature ids. This SQL query shows no result.
Here's my query:
SELECT pl.id_product, pl.id_lang, ml.id_manufacturer, p.active, pl.name as name_product, fp.id_feature as name_attribute, cl.name as category, m.name as brand, pl.meta_description
FROM pr_product p
LEFT JOIN pr_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN pr_category_lang cl ON (cl.id_category = p.id_category_default and cl.id_lang = pl.id_lang)
LEFT JOIN pr_lang l on (l.id_lang = pl.id_lang)
LEFT JOIN pr_manufacturer_lang ml on (l.id_lang = pl.id_lang and l.id_lang = ml.id_lang)
LEFT JOIN pr_manufacturer m on (ml.id_manufacturer = m.id_manufacturer)
LEFT JOIN pr_feature_product fp on (pl.id_product = fp.id_product)
Where l.active = 1 AND pl.id_lang = 2 AND cl.name = 9 and fp.id_feature = 2 AND fp.id_feature = 3 AND fp.id_feature = 35 AND fp.id_feature = 39
Order by p.id_product, pl.id_lang, ml.id_manufacturer, fp.id_feature
The goal is to creat a new meta description in the spreadsheet and import it to the database. In php, the query would be easier, but I have zero knowledge about it.
I'm trying to run a query but my outcome is not what i need.
So the problem is:
A user can be diretor and if this is the case he can see all activities from his department, and he can be user of another department too, in this case not director but only user.
I have 8 departments each with one director, so the following query should give me the activities of the department and the activities of this particular user in other department:
SELECT t1.idAtividade,
t1.idProfessor,
t2.Escola,
t1.Atividade,
t1.Periodo,
t1.Mes,
t1.haveClasses,
t1.DataPrevista,
t1.Destinatarios,
t1.Orcamento,
t1.PdfAtividade,
t1.Avaliacao,
t1.PdfAvaliacao,
t1.idProfessor,
p.Nome,
g.Grupo,
d.Departamento,
p2.Projeto,
t1.idProjeto
FROM atividades AS t1
INNER JOIN professores p on t1.idProfessor = p.idProfessor
INNER JOIN atividadesgrupos ag on t1.idAtividade = ag.idAtividade
INNER JOIN grupos g on ag.idGrupo = g.idGrupo
INNER JOIN departamentosatividades da on t1.idAtividade = da.idAtividade
INNER JOIN departamentos d on da.idDepartamento = d.idDepartamento
INNER JOIN escolas AS t2 ON (t2.idEscola = t1.idEscola)
INNER JOIN anosescolares AS ae ON (t1.idAnoEscolar = ae.idAnoEscolar)
INNER JOIN projetos p2 on t1.idProjeto = p2.idProjeto
WHERE ae.Estado = 1 AND (da.idDepartamento = :id_dpt and ag.idGrupo = :idGrupo)
ORDER BY (t1.idProfessor = :idProfessor) DESC, t1.idProfessor;");
This query is not working because the department have 22 activities but this user (idProfessor) has 5 in this department and 14 more in another department (defined by idGrupo)
I think i will need a subquery right?
I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema
I need some help with this SQL Query. It is designed to retrieve names of students with the same S.S_level values as Jaci Walker, and have taken courses (CS.C_SE_id) with Jaci Walker in the BG building.
I am having trouble on line 7. I need to be able to ensure that the people have enrolled in the same course as Jaci Walker. I'm not sure about what to put in the WHERE statement for that section.
The database schema can be seen here:
SELECT S.S_Fname, S.S_LName
FROM Student S, Enrollment E, CourseSection CS, Location L
WHERE S.S_id = E.S_id
AND E.C_SE_ID = CS.C_SE_id
AND L.Loc_id = CS.Loc_ID
AND S.S_Level = (SELECT S.S_Level FROM Student S WHERE S.S_Fname = "Jaci" AND S.S_Lname = "Walker")
AND CS.C_SE_id = (SELECT CS.C_SE_id FROM CourseSection CS WHERE **?**)
AND L.Loc_id = (SELECT L.Blodg_code FROM Location L WHERE L.Blodg_code = "BG");
I would start by using current SQL-syntax using JOIN conditions instead of using the WHERE clause to show relationships between tables. This way, you get all your table associations done and can better visually confirm you have those elements configured... THEN, tack on the criteria you are looking for.
What I have done here is to just have a PreQuery (result alias "JaciClassesInBG" ) that gets all of Jaci's classes that were enrolled in and ONLY those for the building "BG" (which was added to the JOIN clause to the location table). The WHERE clause was only for Jaci.
From that result, I have a list of all classes that Jaci took. I grabbed her ID, S_Level and C_SE_ID entries.
From that, just join back to the enrollment table of all other students based explicitly on the C_SE_ID that Jaci took (thus all students in that exact same class). However, I've EXCLUDED (via AND NOT...) Jaci's student ID from the list... we know she took the class, we are looking for everyone ELSE.
Finally, join that result back to the students table based on the common enrollment. Now, we can associate the common "S_LEVEL" criteria of Jaci to those students...
Now, you can get whatever details you want for display... in this case, I am grabbing each student, and what class they had in common with Jaci. One student may have been in multiple classes. This will show each. If you only care about one instance, I would just change the top to...
select DISTINCT S2.S_FName, S2.S_LName...
SELECT
JaciClassesInBG.Course_Code,
JaciClassesInBG.Course_Name,
S2.S_FName,
S2.S_LName
from
( SELECT
S.ID,
S.S_Level,
CS.C_SE_ID,
C.Course_Code,
C.Course_Name
FROM
Student S
JOIN Enrollment E
ON S.S_id = E.S_id
JOIN CourseSection CS
ON E.C_SE_ID = CS.C_SE_id
JOIN Location L
ON L.Loc_id = CS.Loc_ID
AND L.Blodg_Code = "BG"
JOIN Course C
ON CS.Course_ID = C.Course_ID
WHERE
S.S_Fname = "Jaci"
AND S.S_Lname = "Walker" ) JaciClassesInBG
JOIN
Enrollment E2
ON JaciClassesInBG.C_SE_ID = E2.C_SE_ID
AND NOT JaciClassesInBG.S_ID = E2.S_ID
JOIN Students S2
ON E2.S_ID = S2.S_ID
AND JaciClassesInBG.S_Level = S2.S_Level
IF I'm understanding the question right this should work:
SELECT s.S_Fname, s.S_LName
FROM Student s
INNER JOIN Enrollment e ON s.S_id = e.S_id
INNER JOIN CourseSection cs ON e.C_SE_ID = cs.C_SE_id
INNER JOIN Location l ON cs.Loc_ID = l.Loc_id
INNER JOIN Student s2 ON s.S_Level = s2.S_Level AND s2.S_Fname = "Jaci" AND s2.S_Lname = "Walker"
INNER JOIN Enrollment e2 ON s2.S_id = e2.S_id
INNER JOIN CourseSection cs2 ON e2.C_SE_ID = cs2.C_SE_id
WHERE l.Loc_id = l.Blodg_code = "BG"
AND cs.Course_ID = cs2.Course_ID
I'm unable to test at the moment and it was quickly done. Maybe a better solution can be found?
Without access to the data I can't test but the following may help (the first inner select is pulling back all the relevant C_SE_ID's)
SELECT S.S_Fname, S.S_Lname
FROM Student S, Enrollment E
WHERE S.S_id = E.S_id
AND E.C_SE_ID IN (SELECT CS.C_SE_ID
FROM Student S2, Enrollment E2, CourseSection CS, Location L
WHERE S2.S_id = E2.S_id
AND E2.C_SE_ID = CS.C_SE_ID
AND CS.Loc_id = L.Loc_id
AND L.Blodg_code = 'BG'
AND S2.S_Fname = 'Jaci'
AND S2.S_Lname = 'Walker')
AND S.S_Level = (SELECT S3.S_Level
FROM Student S3
WHERE S3.S_Fname = 'Jaci'
AND S3.S_Lname = 'Walker')
AND S.S_Fname <> 'Jaci'
AND S.S_Lname <> 'Walker'
I would recommend using different aliases within inner queries (e.g. S2, E2) to avoid confusion.
SELECT snr, nsname FROM type
WHERE snr in (SELECT snr FROM diet
WHERE foodnr in (SELECT foodnr FROM food WHERE foodtype = 'FISH' ));
there are more animals that eat the same kind of food BUT eat different types of food in my database, how do i get the animals that only eat that type of food and nothing else.
bah this is impossible to answer without my entire database i feel like an ass just asking this because its so vague but what i am trying to do is i need to filter out the animals that have more foodtypes besides FISH
JOIN the tables instead, like so:
SELECT t.snr, t.nsname
FROM type t
INNER JOIN diet d ON t.snr = d.snr
INNER JOIN food f ON t.foodnr = f.foodnr
WHERE t.foodtype = 'FISH'
GROUP BY t.foodtype
HAVING COUNT(DISTINCT t.foodtype) = 1;
Note I'm assuming the following about your schema:
TYPE: snr, nsname
DIET: snr, foodnr
FOOD: foodnr, foodtype
You can use a NOT EXISTS:
SELECT t.snr, t.nsname
FROM type t
INNER JOIN diet d ON t.snr = d.snr
INNER JOIN food f ON d.foodnr = f.foodnr
WHERE f.foodtype = 'FISH'
AND NOT EXISTS
(
SELECT 1
FROM diet dd
INNER JOIN food ff ON dd.foodnr = ff.foodnr
WHERE ff.foodtype <> 'FISH'
AND dd.snr = t.snr
)
Or you can use an anti-join pattern:
SELECT t.snr, t.nsname
FROM type t
INNER JOIN diet d ON t.snr = d.snr
INNER JOIN food f ON d.foodnr = f.foodnr AND f.foodtype = 'FISH'
LEFT OUTER JOIN diet dd ON t.snr = dd.snr AND f.foodnr <> dd.foodnr
WHERE f.foodtype = 'FISH'
AND dd.snr IS NULL