Database personal notes (3) - The Basics

## query the database

> When users log on to SQL server 2008, which are designated to a database, usually the master database, but to use our own database, it is necessary to convert. Statement to use database name, database name of which is to select the name of the database for the current database.

`` `sql
example:
use pxscj
Go
(the selected database named pxscj database) `
``
After selecting the database then we begin to learn the query.
The most basic sql statement "select"
rules are as follows:

 

 


Wherein, "*" is used to represent all the rows in a table.
example:

SQL `` `
use pxscj
Go
select *
from xsb
Go
the entire contents of a database query pxscj xsb table
` ``
use the select statement to select certain columns in a table, between the columns to a comma (,) split
cases:

SQL `
the SELECT name, specialty, total credits
from xsb
Go
queries each student's name, specialty, total credits in xsb table
` ``

sql server 2008 can execute multiple queries once.


Column alias can be defined further.
example

`` `sql
query xsb table each student number, name, total credits, and in turn the title is specified as: Number The, name, Mark
the SELECT student number as number, names as name, total credits Mark AS
from xsb
Go

Which can also be used as' = 'for the belt to
Example
number = student number in reverse order
when a substitution box containing names, use' to enclose
embodiment
'Student number' = student number
`

- in conjunction with select where clause

where is the select query, where the statement must be followed in the from clause,
cases

`SQL
case xsb query table Secondary Number '081101' classmates
SELECT *
from xsb
WHERE Student ID = '081101'
` ``
also be used in Comparative
Example

`` `sql
case xsb query table is greater than the total credits of 50 students
SELECT *
from xsb
where total credits> 50
Go
in the where clause may also be incorporated, for example, logical operators and or Not
` ``

Data replacement query

- To replace data in a query, the query case will have to use the expression
- the format is:

 

 

Example:

`` `SQL
use pxscj
Go
the SELECT number, name, rank =
Case
the when the total credits is null then 'not yet elective'
the when the total credits <50 then 'failing'
the when the total credits> = 50 and total credits <= 53 then ' pass'
the else 'excellent'
End
from xsb
the WHERE professional = 'computer'
Go
`` `

- Eliminate duplicate rows
when selecting only some columns in a table, there will be duplicate rows, in order to eliminate duplicate data, we use distinct
cases

SQL `` `
the SELECT DISTINCT student number, total credits
from xsb
` ``

- limit the number of rows in the result set returned

if the select statement returns a result set very much, you can use the TOP option to limit the number of rows returned.

example:

SQL `` `
the SELECT Top 6 name, specialty, total credits
from xsb
Go
` ``
** ** Aggregate Functions

- The most important and most common five: SUM AVG MAX MIN COUNT

`` `sql
cases
select count (*) as 'the total number of students'
from xsb

select sum (score) as 'total course grade 101'
from xsb
the WHERE course = number '101'

select avg (score) as 'Course average score of 101'
from XSB
WHERE course number = '101'
SELECT max (maximum score) as 'Course maximum score 101'
from XSB
WHERE course number = '101'

SELECT min (minimum score ) as 'curriculum minimum score of 101'
from xsb
the WHERE course = number '101'
`` `

- pattern matching (fuzzy search)

 

 

SQL `
the SELECT *
from xsb
the WHERE name like 'King _'
Go
case classmates query xsb table named Wang
` ``

- Compare the range of (in)

 

 

```sql
select *
from xsb
where 专业 in ('计算机','通信工程')
go
查询 xsb 表中专业为 ‘计算机’或‘通信工程’ 专业学生的情况。
```


**下一篇 写子查询**

 

Guess you like

Origin www.cnblogs.com/haoshaoqian/p/11978709.html