[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'
2 6 'CDS'
1 7 'kiy'
3 7 'password'
3 8 'ndf'
Expected result:
A B c
1 3 'CD'
Original Sub-query
SELECT COUNT (*) as Tp_count from Sdb_b2c_orders WHERE pay_status= ' 1 ' and createtime>1413533130 and Area_code in (1030,1 031,1032,1033) and member_id in (select member_id from Sdb_invite_invite where in_member_id=14273 or in_member_id=13742 or in_member_id=14299) LIMIT 1
Execution results are22
Use a connection query instead of the subquery above
SELECT COUNT (*) as Tp_count from Sdb_b2c_orders as Bo INNER joins
There are two tables, A and B.
Aid scriptid taskid yearID
1 5 3 1
2 5 4 1
3 5 5 1
4 6 3 1
5 6 4 1
6 7 3 2
7 7 5 2
8 8 2 2
9 8 6 2
Table B is as follows:Bid aid11 112 213 314 415 516 617 718 819 9
Obtain the maximum value of yearID = 1, scriptid = 5, and taskid not greater than 4 in table.
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
Select
Scriptid,
Max
(Taskid)
As
Taskid
From
A
Where
Ybid
=
1
And
Scripti
The value of a query condition comes from another query.
Like SQL Server sql:
SELECT * from the products where categoryid= (select top 1 CategoryID from categories where categoryname= ' produce ')
The implementation code for this SQL is as follows:
DbSession.Default.From .Where(Products._.CategoryID .SubQueryEqual(DbSession.Default.From .ToList();
Compare the SQL generated by the component
Text: SELECT * FROM [Products] WHERE [Products].[CategoryID] = ( SELECT TO
, the corresponding data is displayed, if not the corresponding two parties can be empty--full join is mainly used in statisticsSELECT * from the staff s full join Department d on d.id = s.department_id;3. Sub-query:--In a subquery in Oracle, ORDER by is not ordered, and all oder by sorts are placed in the final query result--Information on teachers older than Zhangsan--Information for teachers older than the average age-Employee information under the
Label:Execute SQL statement: SELECT * FROM ( SELECT * from tab where id>20 order by UserID Desc As a order by date desc Logically looking pretty right but error: The ORDER by clause is not valid in views, inline functions, derived tables, subqueries, and common table expressions unless you specify also TOP or for XML. As long as we add in the nested subqueries view: Top percent SELECT * FROM ( Select Top Percent * from Tab where id>20 order by UserID Desc As a order by date desc By default,
Recently learning Oracle, want to improve the efficiency and legibility of the written statement, today's note is about the subquery factor words such a thingA factorial query may not necessarily improve efficiency, but it can improve the readability of the program significantly The result of the--with statement with the Sales_c (select Sales,e_no,e_name from Emplyee) SELECT * from sales_c;--query is (select Sales,e_no,e_na Me from Emplyee) The
Tags: course ons stack mysql ber color div for LIS Watch_course_sql="""SelectDate_format (From_unixtime (a.createtime/ +) ,'%%y-%%m-%%d%%t') Regtime, A.username fromBskuser awhereA.usernameinch
(Selectusername frombskchapterlistwhereLessonid=
(SelectID fromBsklesson aWHEREA.lessonname like\'%s\') ) Limit%D -
""" %(Coursename, page* -) the coursename from the front desk should be kanji
But if the error comes when the
background will be incorrect
' u'subquer
table, if you want to use a simple grouping is not possible, the above has been explained, and you can not guarantee that there will be more than the same results.Method One: First use the group to query out the class and the results of the maximum results as a new table, and the original table to jointly query out the corresponding student information.Method Two: If the best results have only one word of the special column, it is possible to sort the result set by class and score before groupi
'. ' St_stock_m 'WHERE(' ma '. ' St_stock_m '. ' Goods_no '='1230480') and (CACHE>(' ma '. ' m '. ' Cell_no ')=' ma '. ' st_stock_m '. ' Cell_no ')))) and(' ma '. ' loc '. ' Cell_no ')=' ma '. ' m '. ' Cell_no ')) Execution plan In fact, the subquery returns no more than 3 result sets, usually we think that the internal will be based on the use of the result set to check, the efficiency will be very fast, but it is not Thought the internal operati
The following articles mainly introduce you to the basic DB2 commands and related knowledge that you must master as a beginner in DB2 database learning. We all know that you are in the DB2 database getting started course, some friends mentioned this question, and experts answered it and compiled it to help other friends learn and master it.
Common
Tags: SQL data performance Oracle--=========================--sql Base--subquery--=========================One, sub-queryA subquery is a query that is inside a select, UPDATE, or DELETE statementIi. classification of sub-queriesSingle-line subqueryReturns 0 rows or one rowMulti-row subqueriesReturn one or more rowsDolez QueryReturn multiple columnsRelated sub-queryReferencing one or more columns in an exter
A subquery is the embedding of a SQL Sever statement into another SQL Sever statement. The database engine performs a query operation on a subquery as a virtual table. Subqueries can be used as a table in a join statement, either as a value in a SELECT statement, as a SQL sever query clause, or as a phrase in a SQL sever query clause, mixed with a data manipulation statement.
The execution of a
This part is simple and clear at a glance.
Five different permission levels: sysadm, sysctrl, sysmaint, dbadm, and loadBut I have read that there is sysmon in CFG, which may be mentioned later (to obtain access to database system monitor data)Or the version is not updated.
The following aspects of the DB2 authorization control database security plan:* Permission level granted to a user* Commands that can be run by users* Allows users to read and/or m
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
This article introduces the usage of different subquery brackets in oracle for 9i and 10g and 11g. if you need this, refer to the following.
There is such a view, probably as follows:
This view can only be compiled successfully in 10g and 11g environments, and cannot be compiled in 9i environments.
The Code is as follows:
Copy code
Select a. *, B .*From (select id, name from)Left join B on a. id = B. id;
The view can be
[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 value of B under each a value, query all records
Case study time:
----------------------------------------------------------------------------------------------------------------------------
1 PRIMARY
Index (NULL) idx_gid 5 (NULL)
Using where
2
Index_subquery id_shop_goods, idx_sid, idx_gid 5 func 1 Using where
.
.
Status = 0 group by gidTo get a large result set t1, the data volume is in rows = 850672.
...... Even if indexes are used in the two queries, it is not surprising that it is not slow.
The execution
1. subquery in SQL:
When we are doing SQL questions, we are most afraid of complex query statements, because most of the time we need two or three subqueries for implementation,
Most of the students are afraid of subqueries. I was confused by these questions,
However, after doing a lot of such questions, we found that there are also some rules. We can sum up a set of methods suitable for ourselves to deal with them,
The following is my solution to thi
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.