the difference exists in mysql and reproduced the original link: https: //blog.csdn.net/zhenwei1994/article/details/82145711

 

1, use:

(1) EXISTS Usage

select a.batchName,a.projectId from ucsc_project_batch a where EXISTS (select b.id from ucsc_project b where a.projectId = b.id)

 

SQL above this means: to ucsc_project_batch main table query batchName and projectId field, which projectId field exists in ucsc_project table.

EXISTS will look ucsc_project_batch circulate that match the query, it does not matter in the child table behind the return of the query What value is there exist only care about the return value, there is a return value, then the condition is true, the article data matching success, adding query results concentrated; no return value if the condition is false, the data is discarded article.

Such as changing all of a sudden we are here for a query returns fields, does not affect the query results outside the query:

select a.batchName,a.projectId from ucsc_project_batch a where EXISTS (select b.companyId,b.name from ucsc_project b where a.projectId = b.id)

 

(2) IN Usage

select a.batchName,a.projectId from ucsc_project_batch a where a.projectId in  (select b.id from ucsc_project b)

 

Above this SQL query results with the results of EXISTS is just the same, the meaning is the same query.

2, notes:

(1) EXISTS written note conditional statement subquery generally need to do to bring off-balance sheet related queries, subqueries conditions otherwise might have been true, false, or has been, carried out when the sheet queries matching cycle, either all check out, or a no.

select a.batchName,a.projectId from ucsc_project_batch a where EXISTS (select b.id from ucsc_project b)

The wording of the above-mentioned example, since the value ucsc_project table exists, the subquery condition has been true, ucsc_project_batch match each data cycle time, can match the success, check out the results became ucsc_project_batch entire table data.

 

select a.batchName,a.projectId from ucsc_project_batch a where EXISTS (select b.id from ucsc_project b where b.id is null)

The wording of the sub-queries do not find out the results, so the subquery condition is false, matching each piece of data outside the query fails, the entire query result is empty

 

(2) IN statement does not limit the number of parameters in mysql, but mysql SQL statement having a length size restrictions, the whole to 4M

What (3) EXISTS sub-query is a query do not care, only care about there is no result set exists, there is the entire sub-queries can be seen as a conditional statement is true, or is a condition for the false statement

(4) IN statement to return to the field only by a sub-queries, or will be error:

select a.batchName,a.projectId from ucsc_project_batch a where a.projectId in  (select b.id,b.companyId from ucsc_project b)

[Err] 1241 - Operand should contain 1 column(s)

3, Scene Selection

Look-up table outside the big, small sub-lookup table, select IN; small look-up tables outside, a large sub-lookup table, select EXISTS; if two tables about the same size, the same.

(1) IN query in SQL query only once, and then the temporary file exists in the result set, and then the outer query matching sql, wherein the outer query with the index can be used subqueries

select a.batchName,a.projectId from ucsc_project_batch a where a.projectId in  (select b.id from ucsc_project b)

 

Equivalent to:

Result = $ [];
$ ucsc_project_batch = "SELECT a.batchName, a.projectId from ucsc_project_batch A";
$ ucsc_project = "SELECT b.id from ucsc_project B";
for ($ I = 0; $ I <$ ucsc_project_batch .length ; $ I ++) {
for ($ J = 0; $ J <$ ucsc_project .length; $ J ++) {
IF ($ ucsc_project_batch [$ I] == $ .projectId ucsc_project [$ J] .id) {
$ Result [] ucsc_project_batch $ = [$ I];
BREAK;
}

(2) foreign EXISTS query table will circulate ucsc_project_batch matching is performed ucsc_project_batch.length views, subqueries may be used wherein the index, outer query full table scan

select a.batchName,a.projectId from ucsc_project_batch a where EXISTS (select b.id from ucsc_project b where a.projectId = b.id)

 

Equivalent to:

Result = $ [];
$ ucsc_project_batch = "SELECT a.batchName, a.projectId from ucsc_project_batch A";
for ($ I = 0; $ I <$ ucsc_project_batch length;. $ I ++) {
IF (EXISTS ($ ucsc_project_batch [$ . i] projectId)) {// perform b.id from ucsc_project B WHERE SELECT = a.projectId b.id
$ Result [] = $ ucsc_project_batch [$ I];
}
}
by two pseudo-code analysis found: subqueries when a large table, use EXISTS can effectively reduce the total number of cycles to increase speed; when large tables when the outer query, using the iN can effectively reduce external loop through a lookup table to improve the speed.
----------------
Disclaimer: This article is CSDN blogger "Huai nineteen months' original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.

 

Guess you like

Origin www.cnblogs.com/syj789/p/12517433.html