Bit by bit [SQL] for the analysis of a series of articles summarizes the bits and pieces of the actual development, from the most simple SQL queries to query the comprehensive analysis
in the analysis of SQL, will be analyzed at the same time related operations mybatis, Hibernate in
clicks see details
This section describes the basic single-table select query statement data
A combination of data query result set (union)
Such demand, pay to organize data from multiple tables together, as a result set is superimposed on another page as, for example, two tables
Table t_user a user table
in Table II department t_dep table
we want to show t_user table user's name and age, and t_dep table user name Age, we can use the union all to multiple rows in tables grouped together
select user_name,user_age
from t_user
union all
select dep_user_name,dep_user_age
from t_dep
Query results are as follows
we can see two duplicate data Joe Smith, if the weight go, we can use the union operator
select user_name,user_age
from t_user
union
select dep_user_name,dep_user_age
from t_dep
The results set UNION operator for combining two or more SELECT statements
UNION ALL UNION command and the command is almost equivalent, but UNION ALL command lists all values
Union: the results of the two sets and set operations, not including duplicates, while default sorting rule;
union All: two result sets and set operations, including duplicates, do not sort;
2 in combination of two tables associated row
For example this case, the user table to display all the user's name, as well as each employee's mood log, the data stored in two separate tables
select u.user_name,d.dep_user_flag
from t_user u,t_dep d
where u.user_id = d.dep_user_id
These two tables to correlate the user id, user table user_id, the department table, dep_user_id corresponding to the user in the user table id.
3 combined data query result set (intersection)
Lookup tables requires two common lines, but a plurality of columns may be used to join the two tables, the following
In MySql and SQL Server, a plurality of coupling conditions may be used to link up with the user table Table dep
select u.user_name as userName,d.dep_user_flag userFlag,d.dep_user_age as age
from t_user u,t_dep d
where
u.user_name = d.dep_user_name
and
u.user_age = d.dep_user_age
Use coupled clause or join on
select u.user_name as userName,d.dep_user_flag userFlag,d.dep_user_age as age
from t_user u
join t_dep d
on (
u.user_name = d.dep_user_name
and
u.user_age = d.dep_user_age)
4 a table query from another table not a value (the complement of two tables)
MySQL and SQL Server
select u.user_name as userName,u.user_age as age
from t_user u
where u.user_name not in (select dep_user_name from t_dep)
Use sub-dep find out the name of the table all the user_name, and then the outer query from the user table lookup sub-query results in no rows of
Oracle 中
select u.user_name from t_user u
minus
select d.dep_user_name from t_dep d
minus two instructions are used in the SQL statement. It first find out the results of the first SQL statement is generated, and then see the results there in the results of the second SQL statement. If so, then this piece of data to be removed, but will not appear in the final results
DB2 and set operations except using PostgreSQL
select u.user_name from t_user u
except
select d.dep_user_name from t_dep d
5 increase connection to the query without affecting other connection
For example, the user information table user, department information sheet dep, employee incentive bonus table table, we need to return all the employee information, they feel the information departments, as well as reward obtained here, not every employee has a department information on the mood, not every employee reward information, but we want to check out all the information, then we can write
In MySQL, DB2, PostgreSQL and SQL Server
select u.user_name,u.user_age,d.dep_user_flag,b.bonus_count
from t_user u
left join t_dep d on d.dep_user_id = u.user_id
left join t_bonus b on b.bonus_user_id = u.user_id
The three tables associated respectively to user_id
end