1

我有一个复杂的联合查询,我想使用 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"
4

0 回答 0