[英]MySQL. Creating View using subquery. Multiple tables
I'm almost there but need a little push. 我快到了,但需要一点推动。
I've got following tables: 我有以下表格:
EMPLEADOS
(employees): EMPLEADOS
(员工):
+--------+----------+------------+----------+------------+---------+----------+--------+
| EMP_NO | APELLIDO | OFICIO | DIRECTOR | FECHA_ALTA | SALARIO | COMISION | DEP_NO |
+--------+----------+------------+----------+------------+---------+----------+--------+
| 7499 | ALONSO | VENDEDOR | 7698 | 1981-02-23 | 1400.00 | 400.00 | 30 |
| 7521 | LOPEZ | EMPLEADO | 7782 | 1981-05-08 | 1350.50 | NULL | 10 |
| 7654 | MARTIN | VENDEDOR | 7698 | 1981-09-28 | 1500.00 | 1600.00 | 30 |
| 7698 | GARRIDO | DIRECTOR | 7839 | 1981-05-01 | 3850.12 | NULL | 30 |
| 7782 | MARTINEZ | DIRECTOR | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7839 | REY | PRESIDENTE | NULL | 1981-11-17 | 6000.00 | NULL | 10 |
| 7844 | CALVO | VENDEDOR | 7698 | 1981-09-08 | 1800.00 | 0.00 | 30 |
| 7876 | GIL | ANALISTA | 7782 | 1982-05-06 | 3350.00 | NULL | 20 |
| 7900 | JIMENEZ | EMPLEADO | 7782 | 1983-03-24 | 1400.00 | NULL | 20 |
+--------+----------+------------+----------+------------+---------+----------+--------+
CLIENTES
(clients): CLIENTES
(客户):
+------------+-------------------------+-----------+-------------+------+-------+----------------+
| CLIENTE_NO | NOMBRE | LOCALIDAD | VENDEDOR_NO | DEBE | HABER | LIMITE_CREDITO |
+------------+-------------------------+-----------+-------------+------+-------+----------------+
| 101 | DISTRIBUCIONES GOMEZ | MADRID | 7499 | 0.00 | 0.00 | 5000.00 |
| 102 | LOGITRONICA S.L | BARCELONA | 7654 | 0.00 | 0.00 | 5000.00 |
| 103 | INDUSTRIAS LACTEAS S.A. | LAS ROZAS | 7844 | 0.00 | 0.00 | 10000.00 |
| 104 | TALLERES ESTESO S.A. | SEVILLA | 7654 | 0.00 | 0.00 | 5000.00 |
| 105 | EDICIONES SANZ | BARCELONA | 7499 | 0.00 | 0.00 | 5000.00 |
| 106 | SIGNOLOGIC S.A. | MADRID | 7654 | 0.00 | 0.00 | 5000.00 |
| 107 | MARTIN Y ASOCIADOS S.L. | ARAVACA | 7844 | 0.00 | 0.00 | 10000.00 |
| 108 | MANUFACTURAS ALI S.A. | SEVILLA | 7654 | 0.00 | 0.00 | 5000.00 |
+------------+-------------------------+-----------+-------------+------+-------+----------------+
I need to create view using columns EMP_NO, APELLIDO and OFICIO from table EMPLEADOS and CLIENTE_NO, NOMBRE from table CLIENTES. 我需要使用表EMPLEADOS中的EMP_NO,APELLIDO和OFICIO列和表CLIENTES中的CLIENTE_NO,NOMBRE创建视图。 The task is to create view of all employees and corresponding to them (assigned) clients (the reference here is emp_no in the EMPLEADOS table is the same as VENDEDOR_NO in table CLIENTES) BUT the list has to include all employees, eve those that they don´t have any clients assigned to them. 任务是创建所有雇员及其对应(分配的)客户的视图(此处的引用是EMPLEADOS表中的emp_no与表CLIENTES中的VENDEDOR_NO相同),但该列表必须包括所有雇员,除以他们所不包括的雇员没有为其分配任何客户。 And this bit is something I am struggling with. 这是我在努力的事情。
What I've got is: 我所拥有的是:
create view V_EMPLEADOS (EMP_NO, APELLIDO, OFICIO, CLIENTE_NO, NOMBRE_CL) AS
-> select em.emp_no, em.apellido, em.oficio, cl.cliente_no, cl.nombre
-> from EMPLEADOS em, CLIENTES cl
-> where em.emp_no = cl.vendedor_no;
But that, of course creates view only of employees that have some clients assigned to them. 但是,这当然只能创建分配了一些客户的员工的视图。 Please help. 请帮忙。
You want a left join
: 您想要left join
:
create view V_EMPLEADOS (EMP_NO, APELLIDO, OFICIO, CLIENTE_NO, NOMBRE_CL) AS
select em.emp_no, em.apellido, em.oficio, cl.cliente_no, cl.nombre
from EMPLEADOS em left join
CLIENTES cl
on em.emp_no = cl.vendedor_no;
Simple rule: Never use commas in the FROM
clause. 简单规则: 请勿在FROM
子句中使用逗号。 Always use proper, explicit JOIN
syntax with the condition in the ON
clause. 始终对ON
子句中的条件使用正确的显式JOIN
语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.