007 database of "check"

Fourth, check

1, a simple query

Query name and age: the SELECT name, Age from STU
Query all columns of data: the SELECT * from STU
Sex query data for men: the SELECT * from STU the WHERE Sex = ' M '

2, comparison operations and queries

Queries Joe's age: the SELECT Age from Students the WHERE name = ' Joe ' 
queries 20 students under the age: the SELECT * from Students the WHERE Age <20 
queries hometown is not in Beijing students: the SELECT * from Students the WHERE Hometown <> ' Beijing '

3, the logic operation and the query

Queries younger than 20 female students: select * from students where age <20 and sex = 'female'
Query or female students 'class 1' students: select * from students where sex = 'female' or class = '1 class'
Tianjin query non-students: select * from students where not hometown = 'Tianjin'

4, fuzzy query

Query surnamed Sun students: the SELECT * from Students the WHERE name like ' Sun% ' 
query surnamed Sun and the name is a word of student: the SELECT * from Students the WHERE name like ' Sun _ ' 
queries student named Joe: the SELECT * from Students the WHERE like name ' % Joe ' 
queries containing the names of white students: the SELECT * from students like the WHERE name ' % white% '

5, range queries

  Queries hometown is a student of Beijing or Shanghai or Guangdong:

* the SELECT from Students the WHERE Hometown in ( ' Beijing ' , ' Shanghai ' , ' Guangdong ' )
the SELECT * from Students the WHERE Hometown not  in ( ' Beijing ' , ' Shanghai ' , ' Guangdong ' )

  Queries for students ages 18-20 are between 18 and 20, the former small value:

select * from students where age between 18 and 20

6, after the elimination of duplicate data query that returns data

Queries the student's age and class, the same data to eliminate 
the SELECT DISTINCT Age, class from Students
to query all student information, student records only once a select distinct
* from students

7, sentenced empty

  Query did not fill out the identity of the student:

select * from students where card is null

8, non-empty

  Judgment of identity cards for the null character

select * from students where card is not null
select * from students where card=''

9. Sort: order by

  All student information queries, by age from small to large: asc: Ascending (default ascending)

select * from students order by age asc

  All student information query, sorted by age descending: desc: descending

select * from students order by age desc

  All student information queries, descending order by age, age is the same, then the student number in ascending order:

select * from students order by age desc,studentNo

10, aggregate functions

count,min,max,sum,avg

The total number of student inquiries: count (card) is not counted as null data
  COUNT the SELECT ( *) AS total number of students from Students
  select count(name) from students
  select count(card) from students
The youngest inquiry girls: the SELECT min (Age) from Students the WHERE Sex = ' female ' 
query class 1 maximum age: the SELECT max (Age) from Students the WHERE class = ' Class 1 ' 
queries sum of the age of students in Beijing: select sum ( Age) from Students
The average age of the girls queries: the SELECT AVG (Age) from Students the WHERE Sex = ' female '

11, groups: group by (after having packet filter)

The number to access a variety of sex: the SELECT Sex, COUNT (*) from Students Group by Sex
The number of queries of all ages: the SELECT Age, COUNT ( *) from Students by Age Group
The total number of queries boys: the same conditions and where the operator behind having
    select sex,count(*) from students group by sex having sex=''
    select count(*) from students where sex=''

12, Page: limit

  From the start began to get count of data, indexes start from zero: the SELECT * from table limit start, count

  M data per page, find: the display data of page n

select * from students limit (n-1)*m,m

  Seeking Pages:
    Query the total number of pieces p1
    use obtained by m p1 p2
    If p2 is divisible by the total number of pages
    , if not evenly divisible by the total number of pages is p2 + 1

 

Note:
int (. 1) does not mean a length
varchar (5) 5-character
decimal (5,2) 3 integers, decimal 2, adding more than the length of the data, automatically rounding
datetime
command-line client, used to view a particular data type use help
help tinyint

 

Guess you like

Origin www.cnblogs.com/qiuniao/p/11964817.html