1. 程式人生 > >mysql8學習手冊第三部分查詢和子查詢

mysql8學習手冊第三部分查詢和子查詢

Selecting data into a file and table

To save the output into a file, you need the FILE privilege. FILE is a global privilege, which means you cannot restrict it for a particular database. However, you can restrict what the user selects:

grant select on employees.* to 'user_ro_file'@'%' identified with msql_native_password as '*EBD9E3BFD1489CA1EB0D2B4F29F6665F321E8C18';

GRANT FILE ON *.* TO 'user_ro_file'@'%' IDENTIFIED WITH mysql_native_password AS '*EBD9E3BFD1489CA1EB0D2B4F29F6665F321E8C18';

The following statement will save the output into a CSV format
select first_name,last_name into outfile 'result.csv' fields terminated by ',' optionally enclosed by'"' lines terminated by '\n' from employees where hire_date <'1986-01-01' limit 10;

預設位置為:/var/lib/mysql/employees

image

image

save as a table

you can save the results of a select statement into a table, Even if the table does not exist,you can use create and select to create the table and load the data. If the table already exists,you can insert and select to load data.

You can save the titles into a new title_only

table;
if the table is not exists
create table titles_only as select distinct title from titles;
else if the tables is exists
insert [ignore] into titles_only select distinct title from titles;

To avoid duplicates,you can use insert igonre. However,in this case,there is no primary key on the titles_only table.So the ignore clause does not make any difference.
為了避免重複,我們可以使用insert ignore 但是在這種情況下,titles_only這張表沒有主鍵,故ignore並沒有效果

Loading data into a table

create table employee_name(
`first_name` varchar(14) not null,
`last_name` varchar(14) not null
)
engine=Innodb;

load data infile 'result.csv' into table employee_name fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';

If the file contains any headers you want to ignore, specify IGNORE n LINES

LOAD DATA INFILE 'result.csv' INTO TABLE
employee_names
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

You can specify REPLACE or IGNORE to deal with duplicates:

LOAD DATA INFILE 'result.csv' REPLACE
INTO TABLE employee_names FIELDS TERMINATED BY
','OPTIONALLY ENCLOSED BY '"' LINES TERMINATED
BY '\n';`

Joining tables 多表連線

you want to find the employee name and department number of a employee with emp_no: 110022

select a.emp_no,a.first_name,a.last_name,c.dept_name from employees a,dept_emp b, departments c 
where a.emp_no=b.emp_no and b.dept_no=c.dept_no and a.emp_no=110022;

you want to find out the average salary for each department

select a.dept_no, avg(b.salary) as avg from dept_emp a,salaries b where a.emp_no=b.emp_no group by (a.dept_no) order by avg desc ;

加入部門名字

select c.dept_name,a.dept_no, avg(b.salary) as avg from dept_emp a,salaries b,departments c where a.emp_no=b.emp_no and a.dept_no =c.dept_no group by (a.dept_no) order by avg desc

image

Identifying Duplicates using SELF JOIN

alter table employees add index name(first_name,last_name);

select emp1.* from employees emp1,employees emp2
where emp1.first_name =emp2.first_name
and emp1.last_name=emp2.last_name
AND emp1.gender=emp2.gender
and emp1.hire_date=emp2.hire_date
and emp1.emp_no!=emp2.emp_no
order by first_name,last_name;


SELECT
emp1.*
FROM
employees emp1
JOIN employees emp2
ON emp1.first_name=emp2.first_name
AND emp1.last_name=emp2.last_name
AND emp1.gender=emp2.gender
AND emp1.hire_date=emp2.hire_date
AND emp1.emp_no!=emp2.emp_no
ORDER BY
first_name, last_name;

Using SUB queries 子查詢

find the name of the employees who started as a Senior Engineer on 1986-06-26 .

 SELECT
first_name,
last_name
FROM
employees
WHERE
emp_no
IN (SELECT emp_no FROM titles WHERE title="Senior Engineer" AND from_date="1986-06-26");


 select a.first_name,a.last_name from employees a, titles b where
a.emp_no=b.emp_no and b.title='Senior Engineer' and b.from_date='1986-06-26';



 select a.first_name,a.last_name from employees a join titles b on a.emp_no=b.emp_no 
where b.title='Senior Engineer' and b.from_date='1986-06-26';

select emp_no from salaries where salary = (select max(salary) from salaries);

Finding mismatched rows between tables

Suppose you want to find rows in a table that are not in other tables. You can achieve this in two ways. Using the NOT IN clause or using OUTER JOIN

image

To understand the usage of OUTER JOIN , create two employee tables and insert some values:

CREATE TABLE employees_list1 AS SELECT * FROM
employees WHERE first_name LIKE 'aa%';

CREATE TABLE employees_list2 AS SELECT * FROM
employees WHERE emp_no BETWEEN 400000 AND 500000 AND
gender='F';
  • find the employees who exist in both lists:
select * from employees_list1 a,employees_list2 b
where a.emp_no=b.emp_no;

select * from employees_list1 a join employees_list2 b
on a.emp_no=b.emp_no;

select * from employees_list1 where emp_no in(
select emp_no from employees_list2);



explain select * from employees_list1 a,employees_list2 b
where a.emp_no=b.emp_no;

explain select * from employees_list1 a join employees_list2 b
on a.emp_no=b.emp_no;

explain select * from employees_list1 where emp_no in(
select emp_no from employees_list2);

使用explain優化查詢:https://blog.csdn.net/zc474235918/article/details/74923614

find out the employees who exist in employees_list1 but not in employees_list2

SELECT * FROM employees_list1 WHERE emp_no NOT IN (SELECT emp_no FROM employees_list2);