Oracle Learning (II) SQL Advanced - table data related

Advanced SQL statements

Top  limit  rownum  Percent  (pieces of data before XXX)

- Top (the SQL Server / the MS Access) 
SELECT  Top number of pieces from the table; 

- Percent (the SQL Server) 
SELECT  Top  20  Percent  *  from Table - Select Data before 20 percent 

- limit (the MySQL) 
SELECT  *  from limit the number of table 

- rownum (the Oracle) 
SELECT  *  from table WHERE rownum <= the number

like (fuzzy query)

- query column G at the beginning of a data value 
SELECT  *  from Table where the column 1 like  ' G% ' ; 

- Query G column data is the end of a 
SELECT  *  from Table where the column 1 like  ' % G ' ; 

- column 1 contains the query data value of G 
SELECT  *  from table where the column 1 like  ' % G% ' ; 

the PS : like in front can be achieved not unselect plus the PS : instead of 0% or more characters _ for a wildcard character

in (specify multiple values)

SELECT  *  from Table where the column in (value 1, value 2, ...);

BETWEEN (the range between the two values can be a number, text, or date)

SELECT  *  from Table where the column between value. 1 and value 2

AS (alias)

select the column as an alias from the table

the Join (connector)

  • The JOIN the INNER : If the table has at least one match, return line
  • JOIN LEFT : even if there is no match in the right table, also returns all rows from the left table
  • JOIN RIGHT : left table even if there is no match, it returns all rows from the right table
  • The JOIN FULL : As long as there is a match where a table, return line

the Join Inner (en)

- Inner the Join keyword return rows when there is at least one match in the table. (Taking A, B intersection) 
the PS : Inner join the join is the same. select columns from Table 1 Inner the Join Table 2 on the table column 1 = Table column 2; the PS : When using join, the difference between on and where the conditions are as follows: 1, on condition is a condition used when generating the temporary table that whether on the condition is true, it will return the records to the left of the table. 2, WHERE condition is good after the temporary table generation, then the temporary table of filter conditions. At this time has no meaning left join in (must return the records left of the table), and the condition is not really all filtered out.   You can view this article explain in detail: the difference between SQL JOIN in on and where the      

the Join left (left connection)

- If there is no match in the right table, the result is null. (Take the left table all the data, this time regardless of whether the table corresponding to the right, the left table there may be many cases) 

the PS : In some databases, LEFT JOIN is called LEFT OUTER JOIN.
PS : LEFT JOIN keyword from the left table (Table 1) returns all rows, even if the right table (Table 2) there is no match. select columns from Table 1 left the Join Table 2 ON column of Table 1 = column of Table 2;

 right the Join (right connection)

- If there is no match left table, the result is null. (Right table to take all the data, this time regardless of whether the corresponding left on the table, the table may be present in the right-to-many cases) 

the PS : In some databases, RIGHT JOIN is called RIGHT OUTER JOIN. 
PS : left RIGHT JOIN keyword table (Table 2) returns all rows, even if the left table (Table 1) does not match. select columns from Table 1 right the Join Table 2 ON column of Table 1 = column of Table 2;

Outer the Join Full (external connection)

- . Full Outer keyword as long as the Join left table (Table 1), and the right table (Table 2) a table where there is a match, the row is returned (return data of all around the table) 

the PS : no matched, not the corresponding column return null, the column has the value of the normal display
PS : MySQL does not support FULL OUTER JOIN. select columns from Table 1 Full Outer the Join Table 2 ON column of Table 1 = Table 2 in the column;

 

Union (combined result set)

  • The UNION : for combining two or more SELECT statements result set.
  • ALL UNION : UNION operator and similarly, by default, UNION operator to select a different value. If you allow duplicate values, use UNION ALL.

PS: Prerequisites when using UNION:

  (1)  the same column  (2) the data type is similar to the column (such as different lengths of the same type)  (3) the sequence have the same

 

- UNION 
the SELECT columns. 1 the FROM TABLE. 1 UNION  the SELECT column 2 the FROM Table 2; 

- UNION ALL 
the SELECT columns. 1 the FROM TABLE. 1 UNION  ALL  the SELECT column 2 the FROM Table 2; 

the PS : the result is always equal to the column name in a first UNION column name a SELECT statement. 
PS : only use a last ORDER BY command, is the two together after the query results, and then sort!

 INTO SELECT (copy table data)

PS: copying data from one table, and another inserting data into the new table , the new table is created automatically inserted.

- Copy all columns into the new table 
SELECT  *  INTO new table FROM old table; 

- copy only the desired columns into a new table 
SELECT column 1, column ... 2 INTO new table FROM old table; 

- copying the plurality of data tables into the new table 
SELECT old column 1. table 1, column 1 of table 2. old, old table 2. column 2
 the iNTO new table
 FROM old table 1 the LEFT  the JOIN 2 old table ON join condition; 

the PS : MySQL database does not support the SELECT INTO statement, but supports INSERT INTO SELECT. 
PS : The new table will use the column names and types defined in the SELECT statement created. We can use the AS clause to define the column name for the new table.

INTO SELECT INSERT (copy table data)

PS: copying data from a table, and then inserting data into another table already exists in.

- copied from all columns of a table into another table already exists 
the INSERT  the INTO existing Table 2 the SELECT  *  the FROM to copy the data in Table 1; 

- copy only the desired column into another existing table 
the INSERT  the INTO table 2 (column 2 of table 1, column 2 of table 2 ...) existing 
 SELECT column 1, row 2 ... the FROM to copy the data in table 1; 

the PS : two different table structure , you can also use the second way to copy data specified column

 

Guess you like

Origin www.cnblogs.com/riches/p/11245420.html