mysql database of single-table queries

Single Standard Query

  Single-table query

  Implementation of priority keywords

  Simple Query

  where constraints

  group by

  Aggregate function

  HAVING filter

  order by ordering inquiry

  LIMIT query to limit the number of records

  Using regular expressions inquiry

Single-table query                                                                                                                                                                    

SELECT DISTINCT Field 1, Field ... 2 the FROM table
                               WHERE conditions for
                               the GROUP BY Field,
                               the HAVING screening
                               the ORDER BY Field,
                               LIMIT limit the number of

Implementation of priority keywords                                                                                                                                                          

from 
where 
Group by 
SELECT 
DISTINCT 
HAVING 
Order by 
limit 

1. Table Found : from
2. holding where specified constraints, to the file / table records an extraction
3. a group of records retrieved Group by , if not by group , a group of the entirety
4. this line SELECT (de-emphasis)
5. the result of the grouping is having filter
6. the results are sorted by the condition: Order by
7. the result limit the number display bar

Simple Query                                                                                                                                                                               

# Simple query 
select field names, field names, field names from the table name ---- name query 

select * from table name ----- Search 

# avoid duplication DISTINCT 
select DISTINCT field name from the table name; 

# define the display format 
    
concat () function is used to string 
SELECT the concat ( " name: " field name) from name; 

first parameter CONCAT_WS () is a separation line 
SELECT CONCAT_WS ( ' : ' , field name) from name; 


# by four operation query 
select field names, salary * 12 (salary * 12) as annual_salary (salary) from the table name # operation plus redefine name

where constraints                                                                                                                                                                             

where words can be used:

1. The comparison operators:> <> = <= <>! =

2.between 80 and 100 # value between 80 and 100

3.in (80,10) # value of 80 or 10

4.like 'e%'

  Wildcards can be a% or _,

  % Represents any number of characters

  _ Represents one character

The logical operators and or not

# Single condition search 
 select field names from table where conditions; 
Example: select the emp_name from Employee where POST = ' Sale ' ; 

# many conditions 

select field names from   Employee where conditions and conditions of 

Example: the SELECT the emp_name, the salary the FROM Employee the WHERE POST = ' Teacher ' the aND the salary> 10000 ; 

# key between and 
Example: the emp_name the SELECT, the salary the FROM Employee 
        the WHERE the salary the BETWEEN 10000 the aND 20000 ; 

    the SELECT the emp_name, the salary the FROM Employee 
        the WHERE the salary the NOT the BETWEEN 10000 20000 the aND; 

# Keywords IS NULL (null determines whether or not a field equal sign, need IS) 
Example: the emp_name the SELECT, the post_comment the FROM Employee 
        the WHERE the post_comment the IS NULL; 

    the SELECT the emp_name, the post_comment the FROM Employee 
        the WHERE the post_comment the IS the NOT NULL; 
note the empty string is not null 

# keyword IN collection query 
Example: the emp_name the SELECT, the salary the FROM Employee 
        the WHERE the salary = the salary = 3000 OR 3500 OR 4000 OR = the salary the salary = 9000 ; 
    
    the SELECT the emp_name, the salary the FROM Employee 
        the WHERE IN the salary ( 3000,3500 , 4000,9000 ); 

    the SELECT emp_name, the Employee salary the FROM 
        the WHERE salary the NOT the IN ( 3000,3500,4000,9000 );
 # keywords like fuzzy query
例:  通配符’%’
    SELECT * FROM employee 
            WHERE emp_name LIKE 'eg%';

    通配符’_’
    SELECT * FROM employee 
            WHERE emp_name LIKE 'al__';
    

group by                                                                                                                                                                                  

# Alone group by grouping keyword 
SELECT POST from Employee group by POST; 


# group by keywords and GROUP_CONCAT () function used in conjunction 

SELECT POST, GROUP_CONCAT (the emp_name) from Employee group by POST;
                 # accordance job packet, and the query groups member name 

# Group by using aggregate functions with 
the SELECT POST, COUNT (the above mentioned id) from the Employee Group by POST;
                 # accordance job grouping, and see how many of each there are 

'' ' 
stressed: 
if we use the field as a unique basis for grouping , each self-contained record set, this packet is not sense 
the same value between a field of a plurality of records, this field is generally used as the basis of a packet 
'' '

Aggregate function                                                                                                                                                                             

# Emphasized: aggregation function polymerized content set, if no packet, a set of default 

Example: 
    the SELECT COUNT ( * ) the FROM Employee; 
    the SELECT COUNT ( *) the FROM Employee the WHERE depart_id =. 1 ; 
    the SELECT MAX (the salary) the FROM Employee; 
    MIN the SELECT (the salary) the FROM Employee; 
    the SELECT the AVG (the salary) the FROM Employee; 
    the SELECT the SUM (the salary) the FROM Employee; 
    the SELECT the SUM (the salary) the WHERE depart_id the FROM Employee =. 3;

HAVING filter                                                                                                                                                                          

HAVING and WHERE is not the same place !!!!!! 

# ! ! ! Execution priority from high to low: WHERE> Group by> HAVING 
# 1. Where occurs before the packet group by, and thus there can Where any field, but must not use the aggregate functions. 
# 2. Having occurs after a packet group by, Having thus can be used in the field of the packet, can not be taken directly to other fields, aggregate functions can be used
mysql> select @@sql_mode;
+--------------------+
| @@sql_mode         |
+--------------------+
| ONLY_FULL_GROUP_BY |
+--------------------+
row in set (0.00 sec)

mysql> select * from emp where salary > 100000;
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | emp_name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
row in set (0.00 sec)

mysql> select post,group_concat(emp_name) from emp group by post having salary > 10000;#错误,分组后无法直接取到salary字段
ERROR 1054 (42S22): Unknown column 'salary' in 'having clause'
mysql> select post,group_concat(emp_name) from emp group by post having avg(salary) > 10000;
+ ----------- + ------------------------------------- ------------------ +
| post | group_concat(emp_name) |
+ ----------- + ------------------------------------- + ------------------ 
| Operation | process bite iron, copper bite Cheng Cheng bite silver, Yaojin, Zhang Ye | 
| Teacher | Chan, jinxin, jingliyang, liwenzhou, yuanhao, wupeiqi, alex | 
+ ----------- + ----------------------------- + -------------------------- 
rows in the SET (0.00 sec) 

 verification
verification

order by ordering inquiry                                                                                                                                                                 

Press single sort 
    the SELECT * the FROM Employee the ORDER BY the salary; 
    the SELECT * the FROM Employee the ORDER BY the salary the ASC;   # ascending 
    the SELECT * the FROM Employee the ORDER BY the salary DESC; # descending 

by multiple column sort: first according to age ordering, if older the same, according to payroll Sort 
    the SELECT * from the Employee 
        the ORDER BY Age, 
        salary DESC;

LIMLT limit the number of records the query                                                                                                                                                       

Example: 
    the SELECT * the FROM Employee the ORDER BY DESC the salary 
        the LIMIT . 3;                     # default initial position is 0 
    
    the SELECT * the FROM Employee the ORDER BY DESC the salary 
        the LIMIT 0, . 5; # starts from 0, i.e., the first query first, then this one comprising including five check back 

    the SELECT * the fROM the Employee the ORDER BY salary DESC 
        LIMIT 5,5; # from the beginning of the fifth, which is to check out the article 6, which then contains a later investigation, including 5

Using regular expressions inquiry                                                                                                                                                                

The SELECT * the FROM Employee the WHERE the emp_name REGEXP, ' ^ ale ' ; # In ale begin 

the SELECT * the FROM Employee the WHERE the emp_name REGEXP, ' on $ ' ; # in on the end of 

the SELECT * the FROM Employee the WHERE the emp_name REGEXP, ' m {2} ' ; # two consecutive a m

 

 

  

Guess you like

Origin www.cnblogs.com/tianshuai1/p/10994335.html