SQL statement problems, associated queries, one-to-many

Source: Internet
Author: User
SQL statement problems, associated queries, one-to-multiple tables for teachers and students, one teacher corresponds to multiple students. I want to find out the students 1, 2, 3, and 4, 5, and 6 corresponding to teacher.


Reply to discussion (solution)

Select * from student Table a, instructor Table B, instructor student relationship Table c where a. studentid = c. studentid and B. teacherid = teacherid and B. teacherid = the instructor id you want to query

What is the table structure?
Table teacher
Id name
1
2 B

Table student
Id tid
1 1
2 1
3 1
4 2
5 2
6 2

Student
Select * from student where tid = 1;

Student B
Select * from student where tid = 2;

Or select a. *, B. name from student as a left join teacher as B on a. tid = B. id order by a. tid;

The instructor has an ID to query all students that meet this ID condition...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.