MySQL创建表/视图

时间:2016-03-15 07:37:52

标签: mysql join sql-view

我不知道标题是否正确,但这是我的问题。 所以,我想在phpmyadmin中创建View,使用来自几个表的数据(下图),View表示灯具的维护,包括来自多个表的字段(变电站,post_type,区域,lamp_type,故障和维护)

以下是连接表:enter image description here

我已经以某种方式管理并创建了这样的东西(下图),我设法使用这段代码创建:

      CREATE OR REPLACE VIEW
v_lamp_I
AS
SELECT
lamps.id,
substation.code AS sub_code,
substation.name AS sub_name,
lamps.lamp_code,
post_type.descript AS post_ty,
lamp_type.descript AS lamp_ty,
area.descript AS area_name,
rasvjeta.adress,
DATE_FORMAT(date_maintenance, "%d.%m.%Y.") AS date_main, 
lamps.geo_long,
lamps.geo_lat
FROM lamps
INNER JOIN substiation ON substiation.id = lamps.substiation_id
INNER JOIN post_type ON post_type.id = lamps.post_type_id
INNER JOIN lamp_type ON lamp_type.id = lamps.lamp_type_id
INNER JOIN area ON area.id = rasvjeta.area_id
INNER JOIN maintenance ON maintenance.lamps_id = lamps.id

enter image description here

我已设法创建视图,但问题在于该视图我只能看到维护的行/灯(sifra_lampe),只有4.在表中 我有24个条目,只有4个条目用于维护。但是,我想看到所有24个条目,如果没有特定灯的维护,字段可以是空的日期格式(00-00-00或它可以是NULL)以及维护的条目/灯我想成为可见的日期字段。

这是带有条目的台灯。 enter image description here

这是维护日期的视图。如您所见,只有6个条目 enter image description here

我希望看到剩下的条目,对于未保留条目的灯可以是null或日期格式(00-00-00),对于维护日期格式的灯可以保持不变,简而言之我想看到所有条目不仅仅是那些被维护的条目。谢谢,对不起,对不起。我不知道如何构建有意义和简短的问题,所以写了一切。

2 个答案:

答案 0 :(得分:1)

CREATE OR REPLACE VIEW
v_lamp_I
AS
select tbl_lamp.*,DATE_FORMAT(date_maintenance, "%d.%m.%Y.") AS date_main 
from (SELECT lamps.id,
substation.code AS sub_code,
substation.name AS sub_name,
lamps.lamp_code,
post_type.descript AS post_ty,
lamp_type.descript AS lamp_ty,
area.descript AS area_name,
rasvjeta.adress,
lamps.geo_long,
lamps.geo_lat
FROM lamps
INNER JOIN substiation ON substiation.id = lamps.substiation_id
INNER JOIN post_type ON post_type.id = lamps.post_type_id
INNER JOIN lamp_type ON lamp_type.id = lamps.lamp_type_id
INNER JOIN area ON area.id = rasvjeta.area_id) as tbl_lamp
LEFT JOIN maintenance ON tbl_lamp.id = maintenance.lamps_id

答案 1 :(得分:1)

create or replace view 
v_lamp_I as 
SELECT lamps.id,
substation.code AS sub_code,
substation.name AS sub_name,
lamps.lamp_code,
post_type.descript AS post_ty,
lamp_type.descript AS lamp_ty,
area.descript AS area_name,
rasvjeta.adress,
lamps.geo_long,
lamps.geo_lat
FROM lamps
INNER JOIN substiation ON substiation.id = lamps.substiation_id
INNER JOIN post_type ON post_type.id = lamps.post_type_id
INNER JOIN lamp_type ON lamp_type.id = lamps.lamp_type_id
INNER JOIN area ON area.id = rasvjeta.area_id;


CREATE OR REPLACE VIEW
v_lamp_new
AS
select v_lamp_I.*,DATE_FORMAT(date_maintenance, "%d.%m.%Y.") AS date_main 
from v_lamp_I
LEFT JOIN maintenance ON v_lamp_I.id = maintenance.lamps_id;