SQL statement issues, correlated queries, one-to-many

Source: Internet
Author: User
For example, the teacher and the student table, a teacher corresponds to several students. Want to find out the teacher A and his corresponding students of the 4,5,6, teacher B and his counterpart.


Reply to discussion (solution)

SELECT * FROM student Table A, teacher Table B, teacher Student relations Table C where A.studentid = C.studentid and B.teacherid = Teacherid and B.teacherid = The teacher ID you want to check

What is the table structure?
Table Teacher
ID Name
1 A
2 B

Table Student
ID TID
1 1
2 1
3 1
4 2
5 2
6 2

A teacher's student
SELECT * from student where tid=1;

B Teacher's students
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 teacher has an ID query for all students who 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.