Home > database >  Consult: select the minimum number of minimum number of each class a student
Consult: select the minimum number of minimum number of each class a student

Time:09-22

Students table
Number of serial number class name
1 1 1 10
2 li 1 10
And the king of 1 3 9
4 10000 9
1 2 zhao 10
2 what 2 10
3 2 countries 9
4 2 gold 9
1 3 10
2, 3, 10 li
3 3 3 king 9
4 30000 9
Choose a per class, number, serial number of the smallest, the result is:
Number of serial number class name
And the king of 1 3 9
3 2 countries 9
3 3 3 king 9
Could you tell me how to write this query? Thank you

CodePudding user response:

1. The first group in class take out least number, serial number of minimum
Select the top 1 * from xx order by number, serial number

2. Merging each class information

CodePudding user response:

First select the minimum number of records in each class set, again through this as a child table choose the smallest number, and then through the class and the minimum number of times the smallest number positioning records can

Select min (b. serial number) as xh, b. b. class, number of times the from student table b, (select d. class as bj, min (d. number) as cishu from students table d group by d. class) where b. c=Arthur c. ishu and b. class=mount j group by b. classes, b. number

CodePudding user response:

1, SELECT the class and grade, the COUNT (*) the class number FROM students table GROUP BY class
2, the class number is 1 and the variable
SELECT top class [number] aa. * FROM table aa, students (class, SELECT COUNT (*) the class number FROM students table GROUP BY class) bb ORDER BY number, serial number WHERE aa. Class=bb. Class
  •  Tags:  
  • VFP
  • Related