Action Query

Basic Connection

select clause list, each target should add the base table name before the column

It shall include all groups from clause tables used

where clause should be equal define a connection

Internal link

The more qualified data tables singled out in the Link is an equivalent connection

The SELECT the SELECT List ( lookup target column)

The INNER table1 the FROM the JOIN (represented by the connection) Table2 [the ON Join_ Conditions (equivalent connections) ]

[WHERE search_ conditions]

[ORRDER BY order_ expression]

 

practice:

SELECT A. class name, A. Class size, B. Name, B. Contact

Class information FROM A   Inner the Join message B counselor

ON A. instructor = B. ID counselor

Where B. Sex = 'female'

inner can be canceled

Left outer join

Select A. class name, A. Class size, B. Name, B. Contact

From the LEFT instructor information B. Outer the Join A class information

ON A. instructor = B. ID counselor

Where A. class size> 20

Ourer can be canceled

Right outer join

Select A. class name, A. Class size, B. Name, B. Contact

From RIGHT class information A. Outer the Join message B counselor

ON A. instructor = B. ID counselor

Where A. class size> 20

Fully connected

Select A. class name, A. Class size, B. Name, B. Contact

From instructor information B FULL outer join class information A.

ON A. instructor = B. ID counselor

Cross-connect

Syntax

Select select_list

FROM table1 CROSS JOIN (连接)table2

[WHERE search_conditions] (screening result)

[ORDeR BY order_expression]

practice

Select A. class name, A. The number, B. Name

Information From A cross join class B information counselor

Since the connection

practice

Select A. class name, A. Class size, B. Class name

From information class A, class B information

Where a. = The number of class B. and A. Class No. Class number <> B. And A. Class No. Class name = "name query class'

Joint inquiry

Syntax

Select select_list

From table_source

[where search_conditions]

{Union [all] (combined with the two query results)

Selectselect_list

From table_source

[where search_conditions]}

[order by order_expression]

all can be canceled, it does not use the results of two queries to filter out duplicate rows

note:

When using UNION queries, connect two result sets must have the same number of expressions in their target list, and the data content as consistent as possible

practice

Select A. No. achievement, A. scores, B. Name

FROM student achievement information A. Information B

Where A. student number = B. Student ID and A. Course ID = '2' and a. Examination ID = '0808'

UNION

Select '' sum (Score) 'Total'

FROM student achievement information A. Information B

Where A. student number = B. Student ID and A. Course ID = '2' and a. Examination ID = '0808'

ORDER BY score

Using Subqueries

Select A. No. achievement, A. scores, B. Name

FROM student achievement information A. Information B

Where A. student number = B. Student ID and A. Course ID = '2' and a. Examination ID = '0808'

And a. Fraction <(

The SELECT   AVG (score) FROM student achievement information A. Information B

Where A. student number = B. Student ID and A. Course ID = '2' and a. Examination ID = '0808'

)

Nested subqueries

Select A. No. achievement, A. scores, B. Name

FROM student achievement information A. Information B

Where A. student number = B. Student ID and A. Course ID = '2' and a. Examination ID = '0808'

And a. Student ID <(

Select Student ID FROM Class B Information A. Student Information

Where A. Class ID = B. Student number and B. relevant class = '2' and a. Instructor = (

Sellect counselor counselor number from information where name = 'Query Name'

xml query

Query Format

practice

FOR XML clause

Table database system can be retrieved by for xml clause and set out to obtain data from the automatic mode represented as XML format.

mode:

RAW mode, AUTO mode, EXPLICIT mode, PATH mode

RAW mode

Select A. class name, A. Class size, B. Name, B. Contact

From class information A. instructor information B

Where A. instructor = B. ID counselor

For cml raw

AUTO mode

Select A. class name, A. Class size, B. Name, B. Contact

From class information A. instructor information B

Where A. instructor = B. ID counselor

For cml auto

EXLSTS keyword query

Select a. * From grade information

Where exists (select * exams Bwhere b. Exam code = a. Exam code and b. Examination ID = '0809')

User Login judgment

Declare  @username varchar (20)

Declare  @pwd varchar(20)

Set @username = '01010202'

Set @pwd = 'ha'

If exists (select * from where student information Student ID = @ yserbane and name = @ owd)

Print 'successful landing'

Else

Print 'login failed'

INTERSECT query cross

Select from a. Achievement information a, scores b. Name achievement information from a, b student information

Where a. Student number = b. Student number and a. Course Code = 2 and a. Examination ID = '0801'

Intersect

Select a. Performance numbers, a. Fraction, A. Name from

(

Select top 10 a. Achievement information a, scores, b. From grade information a, b student information

Where a. Student number = b. Student number and a. Exam Number = '0801'order by a. Fraction desc

)a

 

 EXCEPT poor query

Select from a. Achievement information a, scores b. Name achievement information from a, b student information

Where a. Student number = b. Student number and a. Course Code = 2 and a. Examination ID = '0801'

 

EXCEPT

 

Select a. Performance numbers, a. Fraction, A. Name from

(

Select top 10 a. Achievement information a, scores, b. From grade information a, b student information

Where a. Student number = b. Student number and a. Exam Number = '0801'order by a. Fraction desc

)a

 

 

Published 14 original articles · won praise 0 · Views 659

Guess you like

Origin blog.csdn.net/weixin_42225180/article/details/103398733