Java MySQL database programming Chapter 4 advanced query (II), mysql advanced Query

Source: Internet
Author: User

Java MySQL database programming Chapter 4 advanced query (II), mysql advanced Query
Chapter 4 advanced query (II)

(1) By using EXISTS in the subqueryClause to check whether the row in the subquery exists. Where can a subquery appear in an expression?

(2) Subquery statements can be nested in SQLThe position where any expression appears in the statement.

1. EXISTS subquery

 

1. Use the EXISTS statement to determine whether the database object EXISTS:

Drop table if exists temp;

 

2. As a subquery of the where statement, EXISTS:

SELECT... FROM table name where exists (subquery );

 

3. The parameter following the EXISTS keyword is an arbitrary subquery. If the subquery has rows returned. The result of the EXISTS subquery is true, and then the outer query statement is executed. If any row is returned in the subquery. The result of the EXISTS subquery is false. At this time, the outer query statement is not executed.

Ii. not exists subquery

Like EXISTS and IN, the NOT keyword can also be added for the return operation. not exists indicates that the return operation does NOT exist.

 

Iii. subquery considerations

1) subqueries can be nested in any position where expressions appear in SQL statements.

In SELECTE statements, subqueries can be nested in columns, tables, and query conditions of SELECT statements, that is, SELECT clauses, FROM clauses, WHERE clauses, group by clauses, and HAVING clauses.

 

2) tables that only appear in the subquery but not in the parent query cannot be included in the output column.

The final dataset of a multi-layer nested subquery only contains the fields in the SELECT clause of the parent query. The output result of the subquery is usually used as the data source of its outer subquery or used for data judgment and matching.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.