0

我正在尝试从约会数据库返回一份报告。我附上了我当前的 sql 查询,我遇到的困难是当我“加入”我的客户联系人表时,即

 join contacts on (customer.entity_id = contact.contact_id and contacts.arch = 'contact.phonenumber')
    join contact_detail on (contact_detail.contact_id = contacts.contact_id and contact_detail.lookup = 'mobile')

我会立即失去在该表和详细信息表上没有条目的所有客户,我希望实现的是,如果客户没有联系人条目,它会为这些字段返回空值。即在我添加加入之前它可能已经消失了

Name  Time note    Mobile
DAVID 8pm haircut  0412656865
Julie 8pm style
Daniel 8pm Colour  0412533535

但是添加了这些行

DAVID 8pm haircut  0412656865
Daniel 8pm Colour  0412533535

它不包括朱莉,因为她没有联系电话号码

这就是简单的问题。我希望朱莉返回 NULL 值

该系统使用带有 java 原型驱动前端的 mysql 数据库,因此我们使用实体表、行为表、参与,然后是一些详细信息和查找表。

实际的sql查询是

select 
p.activity_start_time
, p.activity_end_time
, p.activity_start_time AS start_time
, p.activity_end_time as end_time
, a.status
, a.description AS appointmentnote
, e.name as patientname
, e.description AS patientdescription
, customer.name as customername
, customer.description as customerdescription
, sd.name as schedule_name
, eAt.name as appointment_type
, mobile.description as Mobile
from acts a
join participations p on  
a.act_id=p.act_id and a.arch_short_name='act.customerAppointment' and 
p.act_arch_short_name='act.customerAppointment'
join entities e on p.entity_id=e.entity_id
join participations pAt on a.act_id=pAt.act_id and 
a.arch_short_name='act.customerAppointment' and 
pAt.arch_short_name='participation.appointmentType'
join entities eAt on pAt.entity_id=eAt.entity_id
left join entity_relationships er ON (er.target_id = e.entity_id
AND er.active_start_time <= date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "00:00:00" HOUR_SECOND)
AND ((er.active_end_time >= date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "23:59:59" HOUR_SECOND)) OR (er.active_end_time IS NULL)))
left join entities customer ON 
(customer.entity_id = er.source_id AND customer.arch_short_name = 'party.customerperson')

join contacts mobile on (mobile.party_id = customer.entity_id AND mobile.arch_short_name = 'contact.phoneNumber')
join contact_classifications mb_class on (mb_class.contact_id = mobile.contact_id AND mb_class.lookup_id = '120')

join participations schedule on 
a.act_id=schedule.act_id and schedule.arch_short_name='participation.schedule'
left join entities sd on 
(sd.entity_id = schedule.entity_id)
left outer join act_details d on 
a.act_id=d.act_id
where (p.activity_start_time<date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "00:00:00" HOUR_SECOND)
and p.activity_end_time>date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "23:59:59" HOUR_SECOND) or 
p.activity_start_time<date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "00:00:00" HOUR_SECOND)
and p.activity_end_time>date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "23:59:59" HOUR_SECOND) or 
p.activity_start_time>=date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "00:00:00" HOUR_SECOND) and 
p.activity_end_time<=date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "23:59:59" HOUR_SECOND)) and 
(schedule.activity_start_time<date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "00:00:00" HOUR_SECOND) and 
schedule.activity_end_time>date_add((date_format('2013-07-24',"%Y-%m-%d")),
 INTERVAL "23:59:59" HOUR_SECOND) or 
schedule.activity_start_time<date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "00:00:00" HOUR_SECOND) and 
schedule.activity_end_time>date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "23:59:59" HOUR_SECOND) or 
schedule.activity_start_time>=date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "00:00:00" HOUR_SECOND) and 
schedule.activity_end_time<=date_add((date_format('2013-07-24',"%Y-%m-%d")), 
INTERVAL "23:59:59" HOUR_SECOND)) and 
e.arch_short_name = 'party.patientpet' and 
customer.arch_short_name = 'party.customerperson'
and sd.name like CONCAT('GROOM','%')
order by p.activity_start_time, sd.name, a.act_id
4

1 回答 1

1

试试看

join contact_detail on (contact_detail.contact_id = contacts.contact_id and (contact_detail.lookup = 'mobile' or contact_detail.lookup = null))

或(它可能取决于 MySQL 语法)

join contact_detail on (contact_detail.contact_id = contacts.contact_id and (contact_detail.lookup = 'mobile' or contact_detail.lookup is null))
于 2013-07-24T06:02:43.287 回答