Database | SQL queries & LIMIT usage

Foreword

select top n the form of statement can get the first few records check, but this syntax is not mysql, mysql limit use to carry out their functions.

LIMIT clause may be used to force the SELECT statement returns the specified number of records. LIMIT takes one or two numeric parameters. The argument must be an integer constant.

Given two parameters, the first parameter specifies a first offset rows returned, the second argument specifies the maximum number of rows returned.

Age old student table

There are two tables, student tables (student) Basic information is as follows

[Automation / open test series highlights interview] SQL query

Subjects and score table (grade)

[Automation / open test series highlights interview] SQL query

Query the three

Former math lists three students (required to display fields: Student ID, name, subject, results)

select *

from grade

where kemu = 'math'

order by score

desc

limit 3

[Automation / open test series highlights interview] SQL query

First remove the first three records by limit, combined with student-table query

select a.id, a.name, b.kemu, b.score

from student a, grade b

where a.id = b.id

and kemu = 'Mathematics'

order by score

desc

limit 3

The first query 2-3 record

If only a write-back limit integer n, that is, the first n rows of the query; if the latter with two integers n and m, the number n is then first check out queues starting (starting from 0), the second is m is the total number of statistics

Article 2-3 record, then the starting point is 1, 2-3 have two records, then the second parameter is 2

select a.id, a.name, b.kemu, b.score

from student a, grade b

where a.id = b.id

and kemu = 'Mathematics'

order by score

desc

limit 1, 2

[Automation / open test series highlights interview] SQL query

NOTE: limit is based on the number of taken, the same ranking, can be considered a record. If the record of 5-14 take that limit 4 10

Queries third behind all

select a.id, a.name, b.kemu, b.score

from student a, grade b

where a.id = b.id

and kemu = 'Mathematics'

order by score

desc

limit 3, 10000

Note: some of the information written on the limit 3, -1 codes by -1 maximum, this is wrong, will complain, solution: just write a very large integers

[Automation / open test series highlights interview] SQL query

English courses less than 80 people

Statistics English courses less than 80 points, showing Student ID id, name, subject, scores

SELECT a.id, a.name, b.kemu, b.score

FROM student a, grade b

WHERE a.id = b.id

AND b.kemu = 'English'

AND b.score < 80

[Automation / open test series highlights interview] SQL query

Statistics do not pass each course, in general, excellent

Failed course (<60) ships (60 <= x <= 80) Excellent (> 80)

SELECT b.kemu,

(SELECT COUNT(*) FROM grade WHERE score < 60 and kemu = b.kemu) as 不及格,

(SELECT COUNT(*) FROM grade WHERE score between 60 and 80 and kemu = b.kemu) as 一般,

(SELECT COUNT(*) FROM grade WHERE score > 80 and kemu = b.kemu) as 优秀

FROM grade b

GROUP BY kemu

Find results for each subject before 2

Find results for each subject before the two display id, name, subject, scores

Press query subjects and scores

SELECT t1.id, t1.kemu, t1.score

FROM grade t1

ORDER BY t1.kemu,t1.score DESC

[Automation / open test series highlights interview] SQL query

Then find the front of each per Section 2

SELECT t1.id, a.name, t1.kemu, t1.score

FROM grade t1, student a

WHERE

(SELECT count(*) FROM grade t2

WHERE t1.kemu=t2.kemu AND t2.score>=t1.score

)<=2

and a.id = t1.id

ORDER BY t1.kemu,t1.score

DESC

Guess you like

Origin www.cnblogs.com/wyf0518/p/11325174.html