Tutorial 18: Table connection Query
The purpose of this experiment is to master basic joint queries.
Table join
To obtain information from multiple tables, we can join the tables for query under certain conditions.
Related reading:
Basic query statement for Oracle 10 Gb database-aboveBasic query statement for Oracle 10 Gb database-Medium-FunctionBasic query statement for Oracle 10 Gb database-bottom-connection subquery
Cartesian (flute) Connection
W
evening, I executed this SQL statement again on the slave database, and it took 11 minutes and 26 seconds to slow down as the table grows ),........................
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/224P5K43-2.jpg "title =" 3.jpg"/>
The performance of MySQL5.1 and MySQL5.5 sub-queries is quite poor. As a spam fighter, you must use the table connection method to optimize this SQL statement unless MySQL5.6 is just launched this year.
650) this. width = 65
Execute the following hive sql:SELECT * from Trackinfo where ds= $date and session_id in (select session_id from Rcmd_track_path where ds= $date and Add_ca Rt_flag>0 and Product_id>0);The following error is indicated:Failed:parseexception line 2:39 cannot recognize input near ' select ' session_id "from" in expression specificationCause analysis solutions are as follows:Hive does not support the use of in subqueries, consider replacing in with the left semi join, overwriting the above hive SQL
US LIFT joins User_role x on US. pk_id = x.fk_user_idNatural join: Equivalent connectionCommon statements:Update tb5 set name= ' xxxx ' where name= ' XJ ';Update tb5 set gender= ' F ' ORDER by name limit 1;: Only modify the first post sorted with limit 1.Show global variables like ' query_cache% ';: Query cache size, as long as the value greater than 0 is open cache, 0 means cache is offSELECT @ @GLOBAL. query_cache_size;: You can also see if the cache is enabled.Select Current_time ();: Displa
Select A.*From Dept Awhere A.deptno in (select B.deptno from emp b ORDER by B.deptno)--error after Where: Ora-00907:missing right parenthesis;The view behind--from is an inline view, and you can use the order by--such as the escalation of the error statement and then nested a layer into the back of the inline view is good-For example:Select A.*From Dept Awhere A.deptno in (select B.deptnoFrom (select C.deptno to EMP C ORDER by C.deptno) b);A subquery
example One, query out the number of each department, name, location, number of departments, average wage select D.deptno,d.dname,d.loc, ( select count (empno) from emp where Emp.deptno = d.deptno group by Deptno) con, ( select avg (SAL) from emp where Emp.deptno= d.deptno group by Deptno) avgsal from Dept D With clauseexample Two, using the WITH clause to define the data in the EMP table as a temporary table with as (SELECT* from EMP) SELECT * from E;example three , query
MySQL database update subquery
For example:
UPDATE test. tb_vobile aset a. name = '000000' WHEREa. id = (select max (id) id from test. tb_vobile)
Error:
[SQL] UPDATE test. tb_vobile aset. name = '000000' WHEREa. id = (select max (id) id from test. tb_vobile) [Err] 1093-You can't specify target table 'A' for update in FROM clause
You can:
UPDATE test. tb_vobile ajoin (select max (id) id from test. tb_vobile) bon. id = B. idset. name = '20140901
the EMP employee table.Sql:select count (ename) from EMP;Hql:select count (ename) from EMP;Aggregation Functions and meanings:Hql:select avg (esal) from EMP; Salary averageHql:select Max (esal) from EMP; Salary Maximum ValueHql:select sum (esal) from EMP; Minimum wage value Public voidTest91 ()throwsexception{Configuration config=NewConfiguration (). Configure ();Sessionfactory sessionfactory= config.buildsessionfactory ();Session session=NULL;Transaction tr=NULL; Try{Session= sessio
Tags: updatefirst, the demandTo update the Status field for data that begins with Base_info.name to Wl_ to 2Second, the implementationUpdate Base_infoSet a.status=2Where (select id from base_info where name is like '%xx% ')when executing, prompt 1093 error,ERROR 1093 (HY000): You can ' t specify target table ' t ' for update in FROM clause.MySQL does not support modifying a table when subqueries cannot be the same table.Third, optimizeResolve this issue through a temporary tableMethod One:Update
average service duration (in years) for each department's workSelect DEPTNO "department number",Count (EMPNO) "Number of people",ROUND (avg. SAL) "Average salary",ROUND (AVG (Months_between (sysdate, HireDate)/12) "Service life"From EMPGROUP by DEPTNO; --6, list the manager's minimum salary for each department:SELECT Deptno, MIN (SAL) from the emp WHERE job = ' MANAGER ' GROUP by Deptno; --7, giving a salary increase of 10% for persons who have been in office for more than 10 years;SELECT e.*,
Use [erp2015]go/****** object:storedprocedure [dbo]. [GetUser] Script date:03/14/2015 13:27:04 ******/set ansi_nulls ongoset quoted_identifier ongo--=============================== ==============--author:wangyanling--Create date:205-03-12--Description: Get customer information--================================ =============alter PROCEDURE [dbo]. [GetUser]--ADD the parameters for the stored procedure here@uname varchar ($) asbegin--SET NOCOUNT on added to prevent Extra result sets from--inter
Tags: conditional msi order top sum end tar Mon oneResult set of the first query Select* from(
SelectC.msname,a.msid,c.msprice, C.msprice*count (A.MSID) asTotalmoney,sum (A.msamount) asTotalCount fromBillconsume a RIGHT join (SelectTop1Mstime asStarttime,getdate () asEndTime fromBillconsumewhereDateDiff (Dd,mstime,getdate ()) =0ORDER BY mstime ASC) b on a.mstime between B.starttime and B.endtime and a.msid!=0Left join Menus C on C.msid=a.msid GROUP by A.msid, C.msname,c.msprice) s A statement th
Tags: notation col from Port ACK member Div size BSPnot much to say directly on the statement SELECT a.ID, A.thumbnail, A. NAME, A.marketprice, A.memberprice, A.personname, A. STATUS, A.recieveorderdate, A.tracknumber, A.contact, A.reportsenddate, B. Tracknumber, A. Reportdownloadpath from ( SELECT Od.id, Ps.thumbnail, PS. NAME, Od.marketprice, Od.memberprice, Od.personname, od. STATUS, Od.recieveorderdate, Ol.tracknumber, Ol.contact, Od.reportsenddate, Od.reportsendorderlogisticid, Od.report
Tags: src html internet expression com sum logs company archive A few days ago went to two more cattle internet companies interview. In the SQL this block has encountered a problem, eh. Unfortunately, first of all, a simple comb Score Table Score 1. GROUP BY useGroup BY one dimension Like what: Ask for a total score for each student SELECT Student,sum (score) from score GROUP by student Ask for the average score of each student SELECT Student,avg (score) from score GROUP by student can also be
* Query two columns of data from two different tablesMake contact with the same column in the two tables through a subqueryExample: Query all students for Sname, CNO, and degree columns.Select (select Sname from Student where Student.sno=score.sno), Cno,degree from score--where sname is in the Student table, CNO and degree in the score table, where two tables have Student.sno=score.sno connections-- use a table with more data to query the main table, where score is the main tableSecond, aggrega
Tags: style blog http IO SP strong data on 2014Subquery Practice One: Find out all information about people older than 35 years in the sales departmentExercise two: Convert all numeric codes from a department in a haha table to a string display in a bumen tableExercise three: Convert all the number codes from the Department in the haha table to the string display in the Bumen table, and show each person's supervisorConnection query: You can implement multiple table queries by using the Join oper
Label:There is a connection operation between the SQL statements, which must be used in some complex data operations. Simply put, the result of an SQL statement can be part of a connected SQL operation. SQL Structured query statements, subqueries refer to all SQL operations, not a single SELECT statement. 1. Generated by comparison operators: SELECT user_name,user_idfromWHERE>=(SELECTROUND( AVG (age),0 from users); You can use Any,some,all to make queries. SELECT user_name,user_idfromWHERE>= any
cannot generate connection record (extra record)
SelectE1.ename, E2.ename fromEMP E1 Left JoinEMP E2 on(E1.mgr=e2.empno); --full external link, the left and right side of the record can not generate connections to query out
SelectENAME, Dname fromEMP E Full JoinDept D on(E.deptno=D.deptno); --From the connection, the same table will be different aliases, and then as two tables to use. The self-connection can be connected either inside or outside.
SelectE1.ename,e2.ename fromEMP E1JoinEMP E2 on
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.