MySQL study notes

1. Clear Duplicate Lines

  Use the DISTINCT keyword to remove duplicate rows from query results

    

1 SELECT DISTINCT department_id FROM employees;

 

  The scope of DISTINCT is the combination of all the following fields

    

1 SELECT DISTINCT department_id , job_id FROM employees;

 

 

2. String and date values ​​in WHERE

  Strings and dates should be enclosed in single quotes

  Strings are case-sensitive, date values ​​are format-sensitive

1 SELECT last_name, job_id, department_id
2         FROM employees
3              WHERE last_name = ‘KING';                            

 

3. Comparison operators in WHERE

1 SELECT last_name, salary, commission_pct
2     FROM employees
3     WHERE salary<=1500;

 

4. Use the BETWEEN operator to display records in a range of values

1 SELECT last_name, salary
2      FROM employees
3      WHERE salary BETWEEN 1000 AND 1500;

 

 5. Use the IN operator to get records that match list values

1 SELEC Temployee_id, last_name, salary, manager_id
2     FROM employees
3     WHERE manager_id IN (7902, 7566, 7788);

 

6. Use the LIKE operator to perform fuzzy queries

   The query condition can contain literal characters or numbers, (%) can represent zero or more characters, and (_) can represent one character.

1 SELECT last_name
2      FROM employees
3      WHERE last_name LIKE '_A%';

 

7. Logical Operators

7.1 Using the AND operator

  AND requires all conditions to be satisfied T  

1 SELECT employee_id, last_name, job_id, salary
2     FROM employees
3     WHERE salary>=11004 AND job_id='CLERK';

7.2 Using the OR operator

  OR only needs to satisfy one of the two conditions

1 SELECT employee_id, last_name, job_id, salary
2     FROM employees
3     WHERE salary>=1100 OR job_id='CLERK';

7.3 Using the NOT operator

NOT means negate

1 SELECT last_name, job_id
2     FROM employees
3     WHERE job_id  NOT IN ('CLERK','MANAGER','ANALYST');

7.4 Priority Rules

1 SELECT dept_id, salary
2     FROM employees
3     WHERE dept_id=1
4         OR dept_id=2
5         AND salary>1500;

8. Sort query order by

Parameter ASC, which means to sort in ascending order 

Parameter DESC, which means to sort in descending order 

By default, sort in ascending order according to the ASC method

select  *  from employees order  by gender,employee_id ASC ; /*ascending query*/
 select  *  from employees order  by employee_id DESC ; /*descending query*/

9. Subqueries

 1 SELECT
 2     t.NAME
 3 FROM
 4     depts  t
 5 WHERE
 6     t.dept_id = (
 7         SELECT
 8             t1.dept_id
 9         FROM
10             (select * from employees) t1
11         WHERE
12             t1.employee_id = 6
13     );

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326275770&siteId=291194637