- Character matching
statement is as follows:
[NOT] LIKE '<匹配串>' [ESCAPE '<换码字符>'];
NOTE:
<match string> may be a full string may contain wildcards% and _% (Percent sign) represents any length (length may be 0), for example, a string beginning with a% b denotes a, b-terminated character string of arbitrary length
_ (underscore) represents any single character. E.g. a_b represents any string begins with a, b is a length of the end of the 3
[Example 3.29] query learn details of the number of students 201 215 121.
SELECT *
FROM Student
WHERE Sno LIKE ‘201215121';
or
SELECT *
FROM Student
WHERE Sno = ' 201215121 ';
When the string matching is to be noted containing wildcard characters:
- Database character set is ASCII, a character requires two _
- Database character set is GBK when a character needs a _
[Example 3.30] to query all surnamed Liu, student's name, student number and gender.
SELECT Sname, Sno, Ssex
FROM Student
WHERE Sname LIKE '刘%';
[Example 3.31] query name "Ouyang" and the full name of the three characters of the student's name.
SELECT Sname
FROM Student
WHERE Sname LIKE '欧阳_ _';
[Example 3.32] query name in the first two student's name and student number for the word "positive" word.
SELECT Sname,Sno
FROM Student
WHERE Sname LIKE '_ _阳%';
[Example 3.33] query is not all surnamed Liu, student name, student number and gender.
SELECT Sname, Sno, Ssex
FROM Student
WHERE Sname NOT LIKE '刘%';
Use escape wildcard characters escaped as ordinary characters
- ESCAPE '\' means "\" for the escape character
[Example 3.34] query DB_Design course number and course credits.
SELECT Cno,Ccredit
FROM Course
WHERE Cname LIKE 'DB\_Design' ESCAPE '\ ' ;
[Example 3.35] queries that begin with "DB_", and the countdown to the first three characters of the details of the courses i.
SELECT *
FROM Course
WHERE Cname LIKE 'DB\_%i_ _' ESCAPE '\ ' ;
predicate:
- IS NULL 或 IS NOT NULL**
- "IS" can not "=" instead of
[Example 3.36] did not take the test after some elective courses the students, so there are elective record, but did not test scores. Query lack of student achievement and school number corresponding course number.
SELECT Sno,Cno
FROM SC
WHERE Grade IS NULL
[Example 3.37] check all student number and the course number has been fruitful.
SELECT Sno,Cno
FROM SC
WHERE Grade IS NOT NULL;
It can be rewritten as:
SELECT Sname, Ssex
FROM Student
WHERE Sdept= ' CS' OR Sdept= ' MA' OR Sdept= 'IS ';
Logical operators: AND and OR is connected to a plurality of query conditions
- AND higher priority than OR
- You can change the priority brackets
[Example 3.38] queries Department of Computer Science at the age of 20 years old names of the students.
SELECT Sname
FROM Student
WHERE Sdept= 'CS' AND Sage<20;
ORDER BY clause (sorting)
- Can be sorted by one or more attributes columns
- ASC: ASC; Descending: DESC; ASC default value
to a null value, the order of display is achieved by the particular system to determine when ordering
[Example 3.39] query No. 3 elective courses students learn numbers and their results, the query results in descending order by score.
SELECT Sno, Grade
FROM SC
WHERE Cno= ' 3 '
ORDER BY Grade DESC;
[Example 3.40] query all students, the query results by line number where the lines are arranged in ascending order, the same system of student descending order by age.
SELECT *
FROM Student
ORDER BY Sdept, Sage DESC;
Aggregate functions:
- Counting the number of tuples COUNT (*)
- Count the number of a value of COUNT ([DISTINCT | ALL] <column name>)
- Calculating a sum value SUM ([DISTINCT | ALL] <column name>)
- Calculating an average value AVG ([DISTINCT | ALL] <column name>)
- Seeking a maximum and minimum values MAX ([DISTINCT | ALL] <column name>) MIN ([DISTINCT | ALL] <column name>
[Example 3.41] query the total number of students.
SELECT COUNT(*)
FROM Student;
[Example 3.42] query the number of students enrolled in the course.
SELECT COUNT(DISTINCT Sno)
FROM SC;
[Example 3.43] calculation No. 1 course students grade point average.
SELECT AVG(Grade)
FROM SC
WHERE Cno= ' 1 ';
[Example 3.44] student queries highest score elective courses No. 1.
SELECT MAX(Grade)
FROM SC
WHERE Cno='1';
[Example 3.45] Query 201 215 012 elective courses students learn the total score.
SELECT SUM(Ccredit)
FROM SC,Course
WHERE Sno='201215012' AND SC.Cno=Course.Cno;
GROUP BY clause groups:
- Role of the object refinement aggregate function
- If no grouping query results, aggregate functions will be applied to the entire query results
- After the results of the query packet, the aggregate functions are applied to each group
- One or more column values by a specified group, the value is set equal to
[Example 3.46] seeking various elective course number and the corresponding number.
SELECT Cno,COUNT(Sno)
FROM SC
GROUP BY Cno;
[Example 3.47] query more than three elective courses the student number.
SELECT Sno
FROM SC
GROUP BY Sno
HAVING COUNT(*) >3;
[Example 3.48] inquiry average score of 90 points or greater number of students and school grade point average
SELECT Sno, AVG(Grade)
FROM SC
GROUP BY Sno
HAVING AVG(Grade)>=90;
It should be noted
- Switching between the blog and SQL increasingly at ease, rounding the tedious shots, but still have to understand the significance of inter-code line by line