SQL6

 Find last_name and first_name and corresponding dept_name all employees, including temporary employees are not assigned sector of

    the CREATE TABLE `departments` (
    ` dept_no` char (4) the NOT NULL,
    `dept_name` VARCHAR (40) the NOT NULL,
    PRIMARY KEY (` dept_no`));
    the CREATE TABLE dept_emp` `(
    ` emp_no` int (. 11) the NOT NULL,
    `dept_no` char (. 4) the NOT NULL,
    ` from_date` DATE the NOT NULL,
    `to_date` DATE the NOT NULL,
    a PRIMARY KEY (`, emp_no, `,` dept_no`));
    the CREATE TABLE employees` `(
    ` emp_no` int (. 11) the NOT NULL,
    `birth_date` DATE the NOT NULL,
    ` first_name` VARCHAR (14) the NOT NULL,
    `last_name` VARCHAR (16) the NOT NULL ,
    `gender` char (. 1) the NOT NULL,
    ` hire_date` DATE the NOT NULL,
    PRIMARY KEY ( `emp_no`));



meaning of the title is the main query employee information, which includes employee records corresponding to the sector null, so the main table for the employees, using the Join keyword left
1, the first table LEFT JOIN connected employees and dept_emp table, and obtain the corresponding last_name and first_name dept_no all employees, including employees are not assigned sector
2, the second connection table and the LEFT JOIN departments table, i.e. the connection dept_no dept_name, to give all employees and first_name last_name and the corresponding dept_name, not including temporary staff distribution sector

    the SELECT em.last_name, em.first_name, dp.dept_name
    the FROM (the employees EM ON LEFT JOIN dept_emp de em.emp_no = de.emp_no)
    LEFT JOIN ON de.dept_no the departments dp = dp.dept_no
    
may answer this way:

    SELECT e.LAST_NAME, e.first_name, dpp.dept_name 
    from the Employees E left the Join 
    (SELECT * from the Join Departments DP dept_emp de left 
     ON de.dept_no = dp.dept_no) AS DPP ON E. emp_no = dpp.emp_no;

Reference: cow-off network

发布了18 篇原创文章 · 获赞 0 · 访问量 117

Guess you like

Origin blog.csdn.net/axiaomaguohe/article/details/104205608