简体   繁体   中英

How to find result of two table as unique when count of column equal one or two?

I have two table , one name of student and other one have lesson of student , I need to find student with have two or one lesson .

table 1 :(students)
id , name , family , birth , mobile

table 2 : (lessons)
id , studentID , name 

and my code is

SELECT t.* FROM students AS t LEFT JOIN lessons AS tr ON t.id = tr.userID WHERE count(tr.*) = 1

这是您的查询:

SELECT students.name, COUNT(studentID) AS CANT FROM students, lessons WHERE students.id = studentID GROUP BY studentID HAVING CANT = 1 OR CANT = 2

尝试这个

SELECT s.id , s.name , family , birth , mobile FROM student s, lessons l WHERE s.id = l.studentID AND (SELECT COUNT(*) FROM lessons WHERE studentID = student.id) as totalLessons BETWEEN 1 AND 2 GROUP BY s.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM