Laravel:是否可以使用Fluent或Eloquent进行联合查询?

时间:2013-04-18 16:50:19

标签: mysql laravel fluent eloquent

我有一个复杂的联合查询,我想使用Laravel的Fluent查询构建器或Eloquent从MS SQL转换为MySQL(只猜测只有Fluent可以处理它)。另一种选择是简单地使用DB Raw查询,但这似乎不是Laravelish。

我将此作为定期事件引擎的一部分,将过去的事件历史记录表与包含重复发生模式的期货表连接起来。如果您有兴趣,这是查询。

qry = "SELECT clients.p_client_id,contacts.last_name, contacts.first_name, "
qry = qry & "resource_information.last_name as res_last_name, resource_information.first_name as res_first_name, "
qry = qry & "assets.d_name, "
qry = qry & "services.service_name, " 
qry = qry & "service_recurring.id, service_recurring.client_id, service_recurring.partner_id, "
qry = qry & "service_recurring.asset_id, service_recurring.resource_id, service_recurring.service_id, "
qry = qry & "service_recurring.begin_on, service_recurring.window_beg, "
qry = qry & "service_recurring.window_end, service_recurring.rate, "
qry = qry & "service_recurring.discount, service_recurring.res_cost, service_recurring.recurring, service_recurring.id as schedule_id  "
qry = qry & "FROM service_recurring "
qry = qry & "INNER JOIN clients ON service_recurring.client_id = clients.id "
qry = qry & "INNER JOIN contacts ON service_recurring.client_id = contacts.client_id "
qry = qry & "AND contacts.primary_contact = 1 "
qry = qry & "INNER JOIN assets ON service_recurring.asset_id = assets.id "
qry = qry & "INNER JOIN resource_information ON service_recurring.resource_id = resource_information.id "
qry = qry & "INNER JOIN services ON service_recurring.service_id = services.id "
qry = qry & "WHERE service_recurring.partner_id = '" & partner_id & "' "
qry = qry & "AND NOT EXISTS (SELECT * FROM service_recurring_exception WHERE rid = service_recurring.id AND exception_date = '" & thisDate & "' AND pid = '" & partner_id & "') "
qry = qry & "AND service_recurring.begin_on <= '" & thisDate & "' "
qry = qry & "AND (service_recurring.end_on >= '" & thisDate & "' OR service_recurring.end_on is null) "
qry = qry & todayis
qry = qry & "UNION "
qry = qry & "SELECT clients.p_client_id,contacts.last_name, contacts.first_name, "
qry = qry & "resource_information.last_name as res_last_name, resource_information.first_name as res_first_name, "
qry = qry & "assets.d_name, "
qry = qry & "services.service_name, "
qry = qry & "service_history.id, service_history.client_id, service_history.partner_id, "
qry = qry & "service_history.asset_id, service_history.resource_id, service_history.service_id, "
qry = qry & "service_history.begin_on, service_history.window_beg, "
qry = qry & "service_history.window_end, service_history.rate, "
qry = qry & "service_history.discount, service_history.res_cost, service_history.recurring, service_history.id as schedule_id  "
qry = qry & "FROM service_history "
qry = qry & "INNER JOIN clients ON service_history.client_id = clients.id "
qry = qry & "INNER JOIN contacts ON service_history.client_id = contacts.client_id "
qry = qry & "AND contacts.primary_contact = 1 "
qry = qry & "INNER JOIN assets ON service_history.asset_id = assets.id "
qry = qry & "INNER JOIN resource_information ON service_history.resource_id = resource_information.id "
qry = qry & "INNER JOIN services ON service_history.service_id = services.id "
qry = qry & "WHERE service_history.begin_on = '" & thisDate & "' AND service_history.partner_id = '" & partner_id & "' "
qry = qry & "AND service_history.delete_r <> 1 "
qry = qry & "ORDER BY res_last_name, d_name"

0 个答案:

没有答案