MySQL - Limit limit query
For more information about the database, please add attention yo ~ ~. For contact and want to install MySQL please Gabor Lord:
QQ: 3327908431
micro letter: ZDSL1542334210
Introduction: MySQL series of operations fierce as a tiger, the code clear and easy to understand, simple structure, today let's talk about MySQL limit restriction inside the query.
1, Limit grammar
Limit limit the query, by definition, limit the number of bars displayed after the query results, it should be noted that if the use limit 3 represents three records before the restrictions, if the limit 3,5 indicates the start of the display from Article 4 records show a total of five. Due to the wide application of this method, it is classified as a separate article, for your taste!
2, create a table
The story needs to create scores of students scoring form
create table scores (s_id varchar(5),
c_id varchar(3),score float);
insert into score values
("001","01",135),
("005","01",120),
("003","01",110),
("002","01",90),
("005","02",140),
("001","02",125.5),
("004","02",100),
("006","02",90),
("002","03",102),
("005","03",100.6),
("001","03",100),
("003","03",95.6),
("004","03",83),
("003","02",80),
("006","03",79.5);
3, section title
Topic one: The first three tables show student achievement records
select * from scores limit 3;
# 答案 :
001 01 135
005 01 120
003 01 110
Topic two: the top three scores query results information
select * from scores order by score desc limit 3; # 法一
select * from scores order by score desc limit 0,3; # 法二
# 答案 :
005 02 140
001 01 135
001 02 126
Topic three: The third query results to a fifth-place performance information
select * from scores order by score desc limit 2,3;
# 答案 :
001 02 126
005 01 120
003 01 110
Topic Four: Query number of students than 002 students of all course grades are high performance information
analysis: here all grades are higher than 002, 002 must be higher than the highest score, then use the sub-query finds the maximum 002 points, then let's look to score greater than 002 the highest score you can.
select scores.* from scores where score>
(select max(score) from scores where s_id=002); # 法一
select scores.* from scores where score>
(select max(score) from scores group by s_id having s_id=002); # 法二
select scores.* from scores where score>
(select score from scores where s_id=002 order by score desc limit 1);# 法三
# 答案 :
001 01 135
005 01 120
003 01 110
005 02 140
001 02 126
Topic five: three queries before the average score of 100 points or greater student achievement information
select * from scores where s_id in
(select s_id from scores group by s_id having avg(score)>=100)limit 3;
# 答案 :
001 01 135
005 01 120
005 02 140
4, the end of the paper eggs - Kaixinyike
This folks, do not live too tired, and sometimes have to give yourself some fun. This is not just a few days ago Well, me and my friend went to the Cubs to their house. Then his father play it in the phone, we watch TV, at this time it aunt told my uncle to dial the garlic. I'm bored Well, flipped through the phone to see 'China Mobile customer service hotline', I reflected heard to China Mobile to send text messages for free, then sends the information gave China Mobile customer service hotline: "! I miss you", a there will come back: "I want to come to me, ah, the devil." Mother ah! I'm surprised, mobile customer turned out to be alive ... scared I quickly put down the phone ... my uncle. But I think his dad lively, I should probably wondering like his father to learn, so I plan to add his father's micro letter, refused to add the results of such information sent to me: Do not bother me, I already have a family, I love my wife a person for life, even Fan Bingbing came and I love my wife, is such a case.
So your blogger friends, do not let your wife read this article! But you clicked concern, I would not take your cell phone to send text messages, ha ha ha
Today to end here - each article has the final end of the paper eggs - relaxed moment yo Thanks for watching, I was Jetuser-data [learn more knowledge please add MySQL concern]
Links: [https://blog.csdn.net/L1542334210]
CSND: L1542334210
I wish you all success! Family fun!