The basic query and methods, even the tables, subqueries

The basic query and method

  from, where, group by, having, distinct, order by, limit

Even table

  inner join, left join, right join, union

Writing order

  select id,name from emp where id > 3 and id < 6;

Execution order

  from # to determine in the end is what tables
       where # filter the data according to the conditions over
       select # filter out some fields out of the data

  select * from emp \ G; particularly when typesetting table fields when the results might appear confusing phenomenon that you can regulate the query results in a query plus \ G

  Id # 1. query data less than 3 6
      SELECT * from EMP WHERE id> id = 3 and <= 6;
      SELECT * from EMP WHERE id BETWEEN 3 and 6;
      statement above is exactly equivalent to
    # 2 as pay query 20,000 or 18,000 or 17,000 data
      SELECT ID, name EMP WHERE from the salary or the salary = 20000 = 18,000 = 17,000 or the salary;
      SELECT ID, name from the salary in EMP WHERE (20000,18000,17000);

  # 3. Query employee name and salary employees name contains the letter o

  %: Matches any number of characters
        _: matches any character

    select name,salary from emp where name like '%o%';

   # 4. employee name query is a four-character name of the employee's salary its
      SELECT name, from the salary EMP WHERE name like '____';
       # 5. The query id is less than 3 or greater than 6 data
      select * from emp where id <3 ID or>. 6;
      SELECT * from EMP WHERE ID BETWEEN. 3 and not. 6;
      # 6. the query is not salary range 20000,18000,17000 data
      SELECT ID, name from the salary not in EMP WHERE (20000,18000,17000);
      # 7. query job description is empty of staff for the name and job name can only be judged when the null can not be used with = iS
      the SELECT name, POST from the WHERE emp post_comment = null;
      the SELECT name, POST from emp post_comment the WHERE iS null;

MySQL is not case sensitive

group by group

  select * from emp group by post;
    after grouping should do the minimum unit is set, then should not show a single data information within the group
    after group in MySQL can only get field information packet can not obtain other field information directly, but you by other methods (aggregate function) profile acquisition
    if your MySQL not given instructions strict mode is not set
      Show Variables like '% mODE%';
      sET the session of the current window active
      set global globally valid
      set global sql_mode = "strict_trans_tables, only_full_group_by ";
      Group * from EMP by SELECT POST;
      SELECT ID, Group name from EMP by POST;
      SELECT name from Group EMP by POST;
    # 2. Get the maximum salary for each department aggregation function SUM COUNT AVG min max
      SELECT POST, max (the salary) from emp group by post;
    a field alias
      select post as 'sectors', max (salary) as 'maximum wage' from emp group by post;
      select post 'department', max (salary) 'maximum wage' from emp group by post;
    the minimum wage # each department
      select post, min (salary) from emp Group by POST;
    # average salary for each department
      select post, avg (salary) from emp group by post;
    pay the sum of # each department of
      the SELECT POST, sUM (salary) from emp Group by POST;
    # number of people in each department of
      the SELECT POST, COUNT (Age) from emp Group by POST;
      the SELECT POST, COUNT (the salary) from Group EMP by POST;
      SELECT POST, COUNT (ID) Group from EMP by POST;
      SELECT POST, COUNT (the post_comment) from Group EMP by POST;
    the number of packets in the statistical time to fill any non null fields can be done counting, can recommend the use of unique identification data field, such as the id field
    aggregation function will automatically single data within each group do the calculation you want, you do not need to consider the
    name of the department after the query packet and # 3 each All departments under the student's name
      POST SELECT, GROUP_CONCAT (name) from Group EMP by POST;
      SELECT POST, GROUP_CONCAT ( 'the DSB', name) from Group EMP by POST;
      each data field specifies the GROUP_CONCAT () able to get the packet (may be plural) corresponding to the value

   select post,group_concat(name,": ",salary) from emp group by post;

 concat
      select concat("NAME: ",name),concat("SAL: ",salary) from emp;

: Tips
        concat stitching data is used to help you
        use without grouping concat
        use grouping after group_concat

  # Query each employee's annual salary
      select name, salary * 12 from emp ;

  # At first look-up table, be sure to follow the most basic steps, first determine what tables, then check this table to determine no restrictions, and then determine the need for classification, and finally determine what information needs corresponding to the field   
    you should be the results generated by each step are treated as a new table, and then based on that table and then perform other operations
      1. query job name and the names of all staff positions included GROUP_CONCAT
      2. query job name and number of employees in each job included the number of count
      number in the count 3. Search company male employees and female employees
      avg 4. query job name, and the average salary of each position
      5. query job name and the highest salary of each position max
      lowest position 6. queries and name of each position payroll min
      7. Discover the average salary of male employees and male employees, the average salary of female employees and female employees avg
    aggregate function max min sum count avg can only be used after a packet, if a table does not write group by default all data is a set of
    written order of
      select, from, where, group by
    order of execution
      from, the WHERE, group by, the SELECT
    8, statistical departments at the age of 30 More than the average wage of employees
      # to get young workers over the age of 30
      select post, avg (salary) from emp where age> 30 group by post;
    Write sql statement must not breath when finished, follow the steps in a preliminary first step to write, write a query step look at the results and then go down to write the current result based on
    having
        now is where exactly is also used to filter the data, but is now having after the group by
        where is the overall data to do a preliminary screening, but after having the data packet is to conduct a targeted screening
    1, the statistical departments in the age of the average wage of employees over the age of 30,
    and retains the average wage sector greater than 10,000
      SELECT POST, AVG (the salary) from EMP WHERE Age> 30 Group by POST HAVING AVG (the salary)> 10000;
      SELECT POST, AVG (the salary) from EMP WHERE Age> 30 Group by POST WHERE AVG (the salary)> 10000; # error
    # emphasized: having must be used in the group behind by
      SELECT * from EMP HAVING AVG (the salary)> 10000; # given
    execution sequence
      from, WHERE, group by, HAVING, SELECT
    DISTINCT deduplication
    multiple duplicate data a to heavy
    to heavy data must be exactly the same in order to re-
    as long as one is not the same can not be duplicated data
    select distinct id, age from emp;
    execution order
      from, WHERE, by Group, HAVING, SELECT, DISTINCT  
    Order sort by
        default is ascending asc, can also become descending desc  
      SELECT * from EMP Order by the salary;
      SELECT * from EMP Order by salary ASC;
      SELECT * from EMP Order by salary desc;
      SELECT * from EMP Order by age, salary; # first according to age do then follow salary in ascending same ascending age where
      select * from emp order by age asc , salary desc; # first according to age to do and then follow the salary in ascending same ascending age case
    # statistical departments older workers the average wage over the age of 10,
    # and keep departments average salary greater than 1000, and then the average wage sort
      select post, avg (salary) from emp where age> 10 group by post having avg (salary)> 1000 order by avg (salary);
    the number of limit data display limit
      select * from emp limit 5; # show only 25 of the data
      select * from emp limit 5,5; 
    when the limit is only one parameter represents the only show a few
    when the time limit has two parameters indicated by the first argument the second parameter indicates the starting position from the starting position onward show the number
    # tallest man inquiry salary details
    # salary in accordance with the first sort
    and then just take a limit restriction #
      select * from emp order by salary desc limit 1;
    # just seen the beginning reg basically associated with the programming being in
    the regular
      select * from emp where name regexp '       ^ j * (n | y) $.';
    multi-table query
      table query into two categories
          table 1. United query
          2. sub-query
      select * from emp, dep; the result is a Cartesian product
    # queries sector employees and departments of information technology department  
      dedicated to help you do not even approach the table, the connector (inner join ), left connection (left join), the right connection (right join), fully connected (union) # as long as the left and right connections sql statement to add a union becomes fully connected 
          * EMP left from the Join select ON emp.dep_id DEP = dep.id
          Union
          select * from the Join EMP right DEP = ON emp.dep_id dep.id;
    subquery
        query results as a table a further query sql statement
      select DEP from the above mentioned id = the WHERE name (from the SELECT emp dep_id the WHERE name = 'Jason');
    # 2. each department employee newest recruits
    # ideas: each department will check the latest recruits, press the corresponding departments on contingency table query   
    t1.id SELECT, t1.name, t1.hire_date, t1.post, T2. * T1 from EMP AS
        Inner the Join
        (SELECT POST, max (the hire_date) from EMP Group AS MAX_DATE by POST) AS T2
        ON = T2 t1.post .post
        the WHERE t1.hire_date = t2.max_date
        ; 
    # can play an alias to the table
    # can to check out the virtual table aliases
    # can play an alias to the field

Guess you like

Origin www.cnblogs.com/zrh-960906/p/11391849.html