rule to rewrite the matching subquery as an associated query. The following form of the subquery query:
Select * from Tablel
where Column1 in (select column2a from table2 where column2b = value);
You can convert to an associated query that looks like this:
Select Tablel. * FROM Tablel,table2
Where table.column1 = table2.column2a and table2.column2b = value;
(2) The rewriting of mismatched (i.e. missing) su
Label:
A subquery is used to influence the number and order of rows produced in a subquery. For example:
SELECT * fromT1WHERET1.column1inch
(SELECTColumn1 fromT2ORDER bycolumn1); SELECT * fromT1WHERET1.column1inch
(SELECT DISTINCTColumn1 fromT2); SELECT * fromT1WHERE EXISTS
(SELECT * fromT2 LIMIT1); The//limit keyword is not in a subquery tha
Hello everyone, I encountered the following problem in the project: the example of using the combined primary key when the hibernate ing table is as follows: Product table: Item, the code for the primary key as the Union primary key ItemId is as follows: publicclassItemId {@ columnLongitemId; @ columnLonguserId;} @ Table (...) publicclassItem {@ Id
Hello everyone, I encountered the following problem in the project: the example of using the combined primary key when the hibernate ing table is as
[Mysql] An example of subquery bitsCN.com
[Mysql] An example of subquery
Assume that the table my_tbl contains three fields a, B, and c. now we need to query the minimum value of column B under each different value of column a in the table.
For example, the table record is:
A B c
1 3 'CD'
2 3 'nhd'
1 5 'bg
The mysql function replaces the related subquery (Correlated subquery) SQL code CREATE TABLE '2017 _ teacher '('Teacher _ id' int (11) NOT NULL, 'School _ id' int (11) not null, primary key ('Teacher _ id'), KEY '2017 _ teacher_idx_school '('school _ id ')) ENGINE = InnoDB www.2cto.com. there are 1000 teachers in the TABLE, which are randomly distributed in 40 sc
. Rewrite the subquery to the associated query method.1. Rewrite the matching child selection QueryIn the following example, the score table shows the score of the students in the Test Event (T) (excluding the test score !) Query.Select * from score where event_id in (select event_id from event where type = 'T ');It can be seen that the inner layer queries all test events, and the outer layer queries use these test events to get students' scores.This
inner join method, and the query statement can be changed to:
Copy Code code as follows:
SELECT * FROM table1 Innner join table2 using (UUID) where table2.id>=30000;
The native test results are 0.68s.
By explain you can see that MySQL uses simple types (queries other than subqueries or union); The MySQL
mapply.mid = Mconfig.mid
WHERE Mapply.aid
In (
SELECT Aid
From ' mapply ' where state = $state
ORDER BY ' atime ', ' uid ' DESC
)
So no error, MO away thought OK, but after running found that the data have problems
and simply read out the content of the application form is not the same, only to find limit position misplaced, and then put limit in, the results of the following error
This version of the MySQL doesn ' t yet support
Generally, the most basic paging method of MySQL is as follows:
Select * from content order by id desc limit 0, 10
In the case of small data volumes, such SQL statements are enough. The only problem that needs to be paid attention is that indexes are used. As the amount of data increases, the number of pages will increase. The SQL statements on the last few pages may be similar:
Select * from content order by id desc limit 10000, 10
In a word, the
This example describes the MySQL subquery usage. Share to everyone for your reference, specific as follows:
Suppose the table my_tbl contains three field a,b,c; now you need to query the number of records for which column B is the minimum for each different value of column A in the table.
For example, the table record is:
A b C1 3 ' CD '2 3 ' nHD '1 5 ' BG '2 6 ' CDs '1 7 ' Kiy '3 7 ' VSD '3 8 ' NDF '
E
[Mysql] An example of subquery assumes that the table my_tbl contains three fields a, B, and c; now we need to query the minimum value of Column B under each different value of column a in the table. For example, the table record is: a B c1 3 'cd' 2 3 'nhd' 1 5 'bg '2 6 'cd' 1 7'kiy' 3 7'bf'3 8'ndf' expected results is: a B c1 3 'cd' 2 3 'nhd '3 7' _ (1) One of the practices is to first find the minimum val
DBAs or developers who have used Oracle or other relational databases have the experience of thinking that the database has been optimized for the subquery, that it is a good choice to drive the execution of the table, and then porting that experience to the MySQL database, but unfortunately, MySQL may disappoint you in the processing of subqueries, as we encount
Tdb_goods_types as P left joins Tdb_goods_types as s on s.parent_id = P.type_ Id --Find the number of all categories and their subclasses SELECT P.type_id,p.type_name,count (s.type_name) as Children_count from Tdb_goods_types as P left JOIN Tdb_goods_types as s on s.parent_id = p.type_id GROUP by P.type_name ORDER by p.type_id; --Add Child_count field for Tdb_goods_types ALTER TABLE tdb_goods_types ADD child_count mediumint UNSIGNED not NULL DEFAULT
, when we create the index at this time left JOIN .... Is IsNull execution time is more than twice times that of not exisits. To this, about the left JOIN ... Is null and not exists to this end, I need the same next basic conclusion. Left JOIN ... Is null and not EXISTS performance Analysis conclusion: when we need to
;=" .- .- on xx:xx:xx" andPay_time" .- --Geneva A: -: -"Group bypay_id having Count(pay_id)> 1
); Execution result: 2.23 seconds The second way of writing, and the sub-query to join association, this is equivalent to the above in the subquery writing, the following test found that the efficiency is indeed a lot of improvement SelectTpp1.* fromtest_table2 Tpp1, (Selectpay_id fromTest_table2WHEREPay_time
Mysql-join query, mysql-joinThree methods of MySQL association query:
SELECT * FROM film JOIN film_actor ON (film. film_id = film_actor.film_id );SELECT * FROM film JOIN film_actor USING (film_id); -- when the field names of the
right 2 tables + no matching record in table (left field null)Syntax: Select column 1, column 2, column 3, ...From table 1Right join table 2 on table 1. column x = table 2. Column YFull Link: Cartesian product cross join for records in left and right 2 tablesSyntax: Select column 1, column 2, column 3, ...From table 1Cross Join Table 2V. LIMITSyntax: limit [m,]n
execution is a lot more efficient, and you don't need to scan 5.71 million records, it takes only 0.01 seconds to execute.
Query OK, 5 rows affected (0.01 sec)
3. Other suggestions
Although the version of MySQL 5.6 and above is optimized for subqueries, the results of this case are not satisfactory in some cases. Therefore, if you find that some subquery SQL is less efficient, you can try to rewrite t
Tags: Employees customer number Tom CTE order by using sales Mysmysql-Sub-queryA MySQL subquery is a query that is nested within another query. MySQL subqueries can also be nested within another subquery. A MySQL subquery is calle
Label:Reference article: SQL Server Performance Tuning Overview (good summary, don't miss it.)Database: System Database Usage of subqueries A subquery is a select query that is nested within a SELECT, INSERT, UPDATE, DELETE statement, or other subquery. Subqueries can be used anywhere that an expression is allowed to be used. Subqueries can make our programming f
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.