我正在尝试编写一个查询,对于任何预定的事件, 显示活动的详细信息,所选客户以及所有细节 所需的设备。设备可以是内部设备或租用设备。内部和雇用设备是数据库中的两个不同的表。对于事件信息和客户信息的实例,将有内部设备和雇用设备。
我编写的查询使用了union技术。即使表中有明确的信息,查询也会返回“没有返回的行”,如下图所示。
我已经使用AS功能重命名两个表中的不同列,并使用null作为额外列,例如租用设备的租赁费。有人可以帮忙吗?
这是我的代码
COLUMN E_ID FORMAT A7
COLUMN E_NAME FORMAT A35
COLUMN E_STIME FORMAT A21
COLUMN E_FTIME FORMAT A21
COLUMN E_COST FORMAT 99999.99
COLUMN ET_ET_ID FORMAT A5
COLUMN ET_DESC FORMAT A10
COLUMN V_V_ID FORMAT A5
COLUMN V_NAME FORMAT A10
COLUMN C_C_ID FORMAT A5
COLUMN C_FNAME FORMAT A10
COLUMN C_LNAME FORMAT A7
COLUMN C_ORG FORMAT A20
COLUMN C_CNUM FORMAT 9999999
COLUMN EQ_ID FORMAT A5
COLUMN EQ_DESC FORMAT A30
COLUMN EQ_TYPE FORMAT A15
COLUMN EQ_AMT FORMAT A3
COLUMN RENT_FEE FORMAT 9999.99
COLUMN SU_NAME FORMAT A35
BREAK ON E_ID ON E_NAME ON C_C_ID ON C_FNAME ON C_LNAME ON C_ORG ON C_CNUM ON E_STIME ON E_FTIME ON E_COST ON ET_ET_ID ON ET_DESC ON V_V_ID ON V_NAME
SELECT E_ID, E_NAME, EVENT.C_C_ID ,C_FNAME, C_LNAME, C_ORG, C_CNUM, E_STIME, E_FTIME, E_COST, EVENT.ET_ET_ID, EVENTTYPE.ET_DESC, EVENT.V_V_ID, VENUE.V_NAME, INEQUIPMENT.IE_ID AS EQ_ID, INEQUIPMENT.IE_DESC AS EQ_DESC, INEQUIPMENT.IE_TYPE AS EQ_TYPE, EVENT_INEQUIPMENT.IE_AMT AS EQ_AMT, NULL AS RENT_FEE, NULL AS SU_NAME
FROM CUSTOMER, EVENT, EVENT_INEQUIPMENT, INEQUIPMENT, VENUE, EVENTTYPE
WHERE EVENT.E_ID = 'E000001'
AND EVENT.V_V_ID = VENUE.V_ID
AND EVENT.ET_ET_ID = EVENTTYPE.ET_ID
AND EVENT.C_C_ID = CUSTOMER.C_ID
AND EVENT.E_ID = EVENT_INEQUIPMENT.IE_IE_ID
AND EVENT_INEQUIPMENT.IE_IE_ID = INEQUIPMENT.IE_ID
UNION ALL
SELECT E_ID, E_NAME, EVENT.C_C_ID ,C_FNAME, C_LNAME, C_ORG, C_CNUM, E_STIME, E_FTIME, E_COST, EVENT.ET_ET_ID, EVENTTYPE.ET_DESC, EVENT.V_V_ID, VENUE.V_NAME, EXEQUIPMENT.XE_ID AS EQ_ID, EXEQUIPMENT.XE_DESC AS EQ_DESC, EXEQUIPMENT.XE_TYPE AS EQ_TYPE, EVENT_EXEQUIPMENT.XE_AMT AS EQ_AMT, EXEQUIPMENT.R_FEE AS RENT_FEE, SUPPLIER.SU_NAME AS SU_NAME
FROM CUSTOMER, EVENT, EVENT_EXEQUIPMENT, EXEQUIPMENT, SUPPLIER, VENUE, EVENTTYPE
WHERE EVENT.E_ID = 'E000001'
AND EVENT.V_V_ID = VENUE.V_ID
AND EVENT.ET_ET_ID = EVENTTYPE.ET_ID
AND EVENT.C_C_ID = CUSTOMER.C_ID
AND EVENT.E_ID = EVENT_EXEQUIPMENT.XE_XE_ID
AND EVENT_EXEQUIPMENT.XE_XE_ID = EXEQUIPMENT.XE_ID
AND EXEQUIPMENT.SU_SU_ID = SUPPLIER.SU_ID;
您可以从图像中清楚地看到表格中有E_ID = E000001
的数据答案 0 :(得分:1)
你正在进行一系列内部联接。这意味着,对于连接在一起的每个表,您只需要根据连接条件请求在两个表中出现的行。因此,如果您没有从主查询中返回任何行,那么表中某处的数据与您的特定e_id的连接条件不匹配。
首先,我将使用ANSI连接语法重写您的查询,如下所示:
SELECT e_id,
e_name,
event.c_c_id,
c_fname,
c_lname,
c_org,
c_cnum,
e_stime,
e_ftime,
e_cost,
event.et_et_id,
eventtype.et_desc,
event.v_v_id,
venue.v_name,
inequipment.ie_id AS eq_id,
inequipment.ie_desc AS eq_desc,
inequipment.ie_type AS eq_type,
event_inequipment.ie_amt AS eq_amt,
NULL AS rent_fee,
NULL AS su_name
FROM event
INNER JOIN customer ON event.c_c_id = customer.c_id
INNER JOIN event_inequipment ON event.e_id = event_inequipment.ie_ie_id
INNER JOIN inequipment ON event_inequipment.ie_ie_id = inequipment.ie_id
INNER JOIN venue ON event.v_v_id = venue.v_id
INNER JOIN eventtype ON event.et_et_id = eventtype.et_id
WHERE event.e_id = 'E000001'
UNION ALL
SELECT e_id,
e_name,
event.c_c_id,
c_fname,
c_lname,
c_org,
c_cnum,
e_stime,
e_ftime,
e_cost,
event.et_et_id,
eventtype.et_desc,
event.v_v_id,
venue.v_name,
exequipment.xe_id AS eq_id,
exequipment.xe_desc AS eq_desc,
exequipment.xe_type AS eq_type,
event_exequipment.xe_amt AS eq_amt,
exequipment.r_fee AS rent_fee,
supplier.su_name AS su_name
FROM event
INNER JOIN customer ON event.c_c_id = customer.c_id
INNER JOIN event_exequipment ON event.e_id = event_exequipment.xe_xe_id
INNER JOIN exequipment ON event_exequipment.xe_xe_id = exequipment.xe_id
INNER JOIN supplier ON exequipment.su_su_id = supplier.su_id;
INNER JOIN venue ON event.v_v_id = venue.v_id
INNER JOIN eventtype ON event.et_et_id = eventtype.et_id
WHERE event.e_id = 'E000001';
完成后,您可以轻松地将连接转换为左外连接或简单地将它们注释掉,以便您可以诊断哪些连接没有返回任何行。
使用这样的查询,通常有助于从基本表开始,然后逐个添加连接,直到您看到问题所在。