I have a question about selecting data from three tables
the first table is like that:Timetable
ClassID teacherID assistantID
1 1 2
0
2 3 4
` 0 is a break time, so No assistants and teachers
Class table:
classID classname
0 Break
1 MATH
2 ART
teacher table
teacherID teacherName
1 Tom
2 Lily
3 Rose
4 Nile
I want to produce a table like this
Timetables:
ClassName TeacherName AssistantName
Math Tom Liky
Break
Art Rose Nile
Join the teacher table twice with different alias names
select c.className, teach.teacherName, assist.teacherName as assistantName
from Timetable t
join class c on c.classID = t.classID
left join teacher teach on teach.teacherID = t.teacherID
left join teacher assist on assist.teacherID = t.assistantID
Related
I have two tables course and registered_course
course table
crid
crname
crlevel
1
math
senior
2
english
senior
3
physics
senior
Registered course table
id
crid
student_id
1
2
25
2
2
26
3
3
23
4
3
24
5
3
27
so i want to achieved this result the first table join with second table and a count of students that registered a subject just like below thanks
crname
crlevel
number_of_student
math
senior
2
physic
senior
3
SELECT table1.crname, table1.crlevel, count(table2.studentid)
FROM table1
INNER JOIN table2 ON table1.crid = table2.crid
GROUP BY table1.crname, table1.crlevel
I have to say I'm an amateur on MySQL, plus english is not my mother's language.
Here is my first table "Teacher":
ID Name Procedence
1 John Italy
2 Mike Russia
3 Lauren Spain
4 Arnold Spain
And here my second table "Course":
ID1 CourseName
1 Sailing
1 Football
2 Basketball
2 Hockey
I would like to know which country has more teachers giving courses, but I don't want that it count the same teacher twice. So far I made this:
SELECT Procedence, Count(Procedence) as Procedence_Count
from Teacher INNER JOIN Course
ON Course.ID1 = Teacher.ID
GROUP by Procedence
having Procedence_Count > 1;
When I run this, I obtain:
Procedence Procedence_Count
Italy 2
Russia 2
But the code counts John and Mike as two persons, I would like to remove that duplicity, so I would like to obtain:
Procedence Procedence_Count
Italy 1
Russia 1
Thanks a lot.
Use count(distinct):
select Procedence, Count(distinct t.id) as teacher_count
from Teacher t join
Course c
on c.ID1 = t.ID
group by Procedence
having teacher_count > 1;
One additional note: "procedence" is not at all a common English word. One word is "provenance". I think more commonly "origin" or just "country" would be used.
SELECT t1.s_name, count(*) FROM tvSeries AS t1, subTitles AS t2, votes as t3
WHERE
t1.s_id IN (SELECT t2.s_id WHERE sLang='English') AND
t1.s_id IN (SELECT t3.s_id WHERE pts=5) AND
t1.s_id IN (SELECT t3.s_id WHERE uid='britney');
My tvSeries table is like:
s_id s_cat s_name
1 comedy a
2 comedy b
3 drama c
4 comedy d
5 drama e
My subTitles table is like:
s_id sLang
1 English
1 Spanish
2 French
2 English
3 English
1 French
4 German
4 English
5 English
My votes table is like:
s_id uid pts
1 john 4
1 mia 3
1 britney 5
2 rock 5
3 anna 1
3 britney 5
4 megan 3
5 britney 5
I want to select total number of tvSeries and name of tvSeries in this conditions;
which tvSeries gets 5 star from user 'britney' with English subtitles.
When I use my code, I get only one row with number of tvSeries but i want to see many rows with total value. Can anyone help me?
You can do this with simple JOINs (see this answer for an explanation of JOIN vs ,), and then your conditionals are clean and easy to understand.
SELECT
t.s_id,
t.s_name
FROM
tvSeries t
JOIN subTitles s ON s.s_id = t.s_id
JOIN votes v ON v.s_id = t.s_id
WHERE
s.sLang = 'English'
AND v.pts = 5
AND v.uid = 'britney';
If you want just the count of shows instead, you can do:
SELECT
COUNT(*) as count
FROM
...
You can't easily get both the names of the series as well as the count in the same row (because COUNT is an aggregating function), but if you need it you can do:
SELECT
GROUP_CONCAT(t.s_name) as series_names,
COUNT(*) as count
FROM
...
though that returns a single row with concatenated series names (a,c,e) rather than rows which are able to be iterated over.
See http://sqlfiddle.com/#!9/2c252c/13 for a working example.
I have two tables. One is the Course table and the second is the Teacher table. I want to get all Teacher who does not teach 'Math'. How can I do this?
Course Table
course_id course teacher_id marks
1 Physics 1 60
2 Math 1 60
3 Chemestry 1 60
4 English 2 60
5 Hindi 2 60
6 Physics 2 60
7 Chemestry 3 60
8 English 4 60
9 Math 5 60
10 Math 6 60
Teacher Table
teacher_id name salary gender
1 Teacher1 20 1
2 Teacher2 30 1
3 Teacher3 40 2
4 Teacher4 50 2
5 Teacher5 60 1
6 Teacher6 70 2
I want to get all teacher who does not teachs math.
You need to join both the tables on teacher_id and then filter out the rows based on the course.
SQL> SELECT DISTINCT t.name
2 FROM course c,
3 teacher t
4 WHERE c.teacher_id = t.teacher_id
5 AND c.course <> 'Math';
NAME
--------
Teacher2
Teacher1
Teacher4
Teacher3
SQL>
EDIT Since you have teachers teaching multiple courses, you need to filter out further:
SQL> WITH DATA AS
2 (SELECT c.*,
3 t.name
4 FROM course c,
5 teacher t
6 WHERE c.teacher_id = t.teacher_id
7 AND c.course <> 'Math'
8 )
9 SELECT DISTINCT name
10 FROM data
11 WHERE teacher_id NOT IN
12 (SELECT teacher_id FROM course WHERE course = 'Math'
13 )
14 /
NAME
--------
Teacher2
Teacher4
Teacher3
SQL>
NOTE Please keep in mind that the other solution using NOT EXISTS clause is better in terms of performance, since the table scans are less and even index scans. With proper indexes, the not exists query would be an optimal method.
select *
from teacher t
where not exists
(select 1 from course c where c.teacher_id = t.teacher_id and c.course = 'Math')
#LalitKumarB
Ben is absolutely right
inner join
select t.teacher_id, t.name
from teacher t, Course c
where c.course='math' and t.teacher_id=c.teacher_id;
EDIT
you can do it using join and subquery.
select * from course join teacher
on course.teacher_id=teacher.teacher_id
where teacher.teacher_id not in
(select distinct teacher_id from course where course = 'Math')
Select * from Teacher
join Course
on Teacher.teacher.id = Course.teacher.id
where Course.course != 'Math'
select
t.name
from teacher t
left join course c
on c.teacher_id = t.teacher_id
where c.course_id <> 2
I Have seven tables I am trying to join:
Table programs
Column id program_name program_description
1 Self Help Self Help Description...
2 Wellness Wellness Description...
3 Education Education Description...
______________________________________________________
Table county
Column id county_name
1 Stark
2 Portage
3 Wayne
_________________________
Table services
Column id service_name service_description
1 Counseling Counseling Description...
2 Group Therapy Group Therapy Description...
3 Evaluation Evaluation Description...
__________________________________________________________
Table population
Column id population_name
1 Adults
2 Children
3 Youth
_____________________________
Table program_county
Column id program_id county_id
1 1 2
2 1 3
3 2 1
4 2 2
_____________________________________
Table program_service
Column id program_id service_id
1 1 2
2 1 3
3 2 1
4 2 2
5 2 3
______________________________________
Table program_population
Column id program_id population_id
1 1 3
2 2 1
3 2 3
4 3 1
_________________________________________
I am trying to write a query that would return one row for each program and retrieve the related rows in program_county,program_services, and program_population tables and lookup the names of those services, population and counties display them in one field each. Like:
id program_name program_description Counties Services Population Served
1 Self Help Self Help Description... Portage, Wayne Group Therapy, Evaluation Youth
2 Wellness Wellness Description Stark, Portage Counseling, Group Therapy, Evaluation Adults, Youth
I know I have to use joins and GROUP_CONCAT but I am admittedly very lost.
You'll want to use outer joins if you want to see all programs regardless of whether they are associated to counties, services, or populations. Likewise you should use coalesce() around the values you are concatenating to handle NULL values properly.
Something like this should work:
select programs.id, programs.program_name, programs.program_description,
group_concat(distinct coalesce(county.county_name,'')) as "Counties",
group_concat(distinct coalesce(services.service_name,'')) as "Services",
group_concat(distinct coalesce(population.population_name,'')) as "Population Served"
from programs
left outer join program_county on program_county.program_id = programs.id
left outer join county on county.id = program_county.county_id
left outer join program_service on program_service.program_id = programs.id
left outer join services on services.id = program_service.service_id
left outer join program_population on program_population.program_id = programs.id
left outer join population on population.id = program_population.population_id
group by programs.id, programs.program_name, programs.program_description
order by programs.id