The first thing to consider when using subqueries is that subqueries can be nested in multiple tiers and subqueries () enclosed by parentheses (), let's take a look at the detailed descriptions below.
Basic introduction
1,wherer: A subquery typically returns a single row of more than a single row of multiple columns;
2,having: The subquery Returns a single col
Briefly:In the query statement contains another query statement, the contained query is called a subquery, the query containing the subquery is called the parent query.In summary, a subquery is one or more query statements nested within a query statement.
Common sub-query categories:One, independent sub-queryFeat
can be nested, with the latest layer of queries first executed. Subqueries can be used in select, insert, update, delete, and other statements.Subqueries can be divided into single-row subqueries, multi-row subqueries, and multi-column subqueries based on the type of returned data.
Single Row subqueryQuery the names and salaries of employees who are higher than Scott.Run the following query:
SQLCode
1.SelectEname, SalFromEMP
2.WhereSAL> (Sele
MySQl subquery, left-right join, and multi-table join learning Notes
1. subquery refers to the SELECT clause in another query statement.
Example:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2 );
SELECT * FROM t1... is called Outer Query [external Query] (or Outer Statement ),
SELECT column1 FROM t2 is called Sub Query [subquery].
Therefore, subquerie
while the foreign key is being constructed.(4) When updating a primary key record, there is also a selection of cascading updates and rejected executions.3.2 SettingsSelect the table, right-click: Design, select the field that you want to set the foreign key, choose Relationships, select Add, select the right ellipsis, and select the appropriate relationship at the table and column specifications3 Sub-queryIn the SQL language, when one query statement is ne
Subquery definition: Using a SELECT statement inside an external SQL statement, this SELECT statement is called a subquerySub-query type:Single-line subquery: Either no results are returned for external SQL, or only one row is returned. Single-row subquery special case: Is this number of queries a column, this seed query is called"Scalar Quantum Query"Multiline
Label: Block Nested-loop and batched Key Access Joins The batched Key access (BKA) join algorithm accesses the joined table through index and join buffer, and the BKA algorithm supports inner Join,outer join and semi join operations, including nested outer joins, The benefits of BKA include increased join performance (due to a more efficient table scan), and the addition of the Block
, Class.class_name fromClassINNER JOINRosterWHEREClass.class_num=Roster.class_num; +-----------+------------+|Class_num|Class_name|+-----------+------------+| 2 |Class2 || 3 |Class3 || 3 |Class3 |+-----------+------------+3Rowsinch Set(0.00sec)Duplicate values can be removed through distinct, but this affects performance, so results are obtained through subqueriesMysql> SELECTClass_num, Class_name fromClassWHEREClass_numinch(SELECTClass_num fromroster); +-------
Oracle (4) --) Advanced subquery ~)
This section will discuss the more complex application of instant queries, mainly to simplify SQL and improve performance from the aspect, of course .. There are many ways to improve performance .. Today I attended a lecture on Oracle performance tuning and learned about all aspects of Oracle performance and optimization. Although we may not be able to deal with such complex problems, we can still optimize SQL state
Oracle (4) --> advanced subquery (it is difficult to explain multiple examples, but I really don't believe it ~), Oracle Introduction
This section will discuss the more complex application of instant queries, mainly to simplify SQL and improve performance from the aspect, of course .. There are many ways to improve performance .. Today I attended a lecture on Oracle performance tuning and learned about all aspects of Oracle performance and optimizatio
expanded based on standard SQL to achieve efficient database applications.
With as is a statement structure frequently used in Oracle SQL. Especially in some data warehouse mining systems, the SQL statement structure and nested structure are very complicated. With as can help us simplify the statement structure, improve statement readability, and improve statement execution plan control.
The WITH syntax is added to Oracle 9i to name the
Sub-querySub-query refers to a complete query, nested in a number of different functions of small queries, so as to complete a complex query a form of writing, in order to let the reader more clearly understand the concept of sub-query.Sub-query returns resultsThere are four types of data that can be returned by subqueries:
Single row: Returns the contents of a specific column, which can be understood as a single-valued data;
MySQL does not support subquery optimization.
1. Create a table
Create table tt1 (id int primary key, c1 INT );Create table tt2 (id int primary key, c2 INT );Insert into tt1 values );Insert into tt2 values (1, 2), (2, 2 );
2. Execution Plan and Problems
Mysql> explain extended select tt1.c1, (SELECT tt2.c2 FROM tt2 WHERE c2 = 10) FROM tt1, tt2;+ ---- + ------------- + ------- + ------------- + --------- + ------ + ---------- + ------------------------
column of new records3) insert data into the new column. The data must correspond to each other.-- This operation allows the information of different tables to be centralized into one table2. Related deletion-use the relevant subquery to delete data from another table based on the data in one table. For example, delete the employees table and all the data in the emp_history table. 1) Create the emp022 table, add data in the department_id of the emplo
Many developers prefer subquery instead of join. For them, subquery is easier to implement. However, in many cases, join is better than subquery.
First, let's take a look at subquery: subqueries are also called internal queries or internal choices, while statements containing subqueries are also called external querie
1. Create a table createtablett1 (idintprimarykey, c1INT); createtablett2 (idintprimarykey, c2INT); insertintott1value
Create table tt1 (id int primary key, c1 INT); create table tt2 (id int primary key, c2 INT); insert into tt1 value
1. Create a table
Create table tt1 (id int primary key, c1 INT );Create table tt2 (id int primary key, c2 INT );Insert into tt1 values );Insert into tt2 values (1, 2), (2, 2 );
2. Execution Plan and Problems
Mysql> explain extended select tt1.c1, (SELECT tt2.c
1.EXISTS EXISTS (subquery)The EXISTS argument is an arbitrary SELECT statement, or subquery. The system operates a subquery to determine whether it returns rows. If it returns at least one row, the result of the EXISTS is true, and if the subquery does not return rows, then the EXISTS result is "false."
Subqueries can
is useful when you want to replace a basic table with a view, but it is best not to do so. For more information, see the following.
(21)
abstract(Optional): Used inIn hierarchies.
Pay attention to the red font, which is the key. Next I found the relevant content:
For hibernate ing, views and tables are no different because they are transparent at the data layer (Note: some databases do not support view attributes, especially when updating ). Sometimes you want to use a view,
One, the child chooses the basic usage
1, the definition of the sub-selection
The sub-option allows one query to be nested within another query. For example, an examination score project divides the examination into two situations: examination (T) and test (Q). The following query will only find the students ' test scores.
SELECT * FROM score where event_id the (select event_id from event where type= ' T ');
2, the use of sub-selection (3 kinds)
Use s
table are displayed in the order in which they are entered.The primary key features: Uniqueness, non-nullability.Example of setting a primary KEY statement:Code int primary Key, primary key cannot be empty, cannot be duplicated, ensures uniquenessExample of setting a self-growing primary KEY statement:Code int primary key identity (1), 1 increase each time, add values without adding this columnTo set a foreign key:Right-click on the table where you want to set the foreign key, select design, an
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.