Note that there are some explanatory texts on larger screens.

plurals
  1. POLaravel: Is it possible to do a union query with Fluent or Eloquent?
    primarykey
    data
    text
    <p>I have a complex union query that I would like to translate from MS SQL to MySQL using either Laravel's Fluent query builder or Eloquent (just guessing only Fluent could handle it). The other alternative would be to simply us a DB Raw query, but that doesn't seem very Laravelish.</p> <p>I do this as part of a recurring events engine to connect a past events history table with the futures table that holds the recurrence pattern. Here's the query if you're interested.</p> <pre><code>qry = "SELECT clients.p_client_id,contacts.last_name, contacts.first_name, " qry = qry &amp; "resource_information.last_name as res_last_name, resource_information.first_name as res_first_name, " qry = qry &amp; "assets.d_name, " qry = qry &amp; "services.service_name, " qry = qry &amp; "service_recurring.id, service_recurring.client_id, service_recurring.partner_id, " qry = qry &amp; "service_recurring.asset_id, service_recurring.resource_id, service_recurring.service_id, " qry = qry &amp; "service_recurring.begin_on, service_recurring.window_beg, " qry = qry &amp; "service_recurring.window_end, service_recurring.rate, " qry = qry &amp; "service_recurring.discount, service_recurring.res_cost, service_recurring.recurring, service_recurring.id as schedule_id " qry = qry &amp; "FROM service_recurring " qry = qry &amp; "INNER JOIN clients ON service_recurring.client_id = clients.id " qry = qry &amp; "INNER JOIN contacts ON service_recurring.client_id = contacts.client_id " qry = qry &amp; "AND contacts.primary_contact = 1 " qry = qry &amp; "INNER JOIN assets ON service_recurring.asset_id = assets.id " qry = qry &amp; "INNER JOIN resource_information ON service_recurring.resource_id = resource_information.id " qry = qry &amp; "INNER JOIN services ON service_recurring.service_id = services.id " qry = qry &amp; "WHERE service_recurring.partner_id = '" &amp; partner_id &amp; "' " qry = qry &amp; "AND NOT EXISTS (SELECT * FROM service_recurring_exception WHERE rid = service_recurring.id AND exception_date = '" &amp; thisDate &amp; "' AND pid = '" &amp; partner_id &amp; "') " qry = qry &amp; "AND service_recurring.begin_on &lt;= '" &amp; thisDate &amp; "' " qry = qry &amp; "AND (service_recurring.end_on &gt;= '" &amp; thisDate &amp; "' OR service_recurring.end_on is null) " qry = qry &amp; todayis qry = qry &amp; "UNION " qry = qry &amp; "SELECT clients.p_client_id,contacts.last_name, contacts.first_name, " qry = qry &amp; "resource_information.last_name as res_last_name, resource_information.first_name as res_first_name, " qry = qry &amp; "assets.d_name, " qry = qry &amp; "services.service_name, " qry = qry &amp; "service_history.id, service_history.client_id, service_history.partner_id, " qry = qry &amp; "service_history.asset_id, service_history.resource_id, service_history.service_id, " qry = qry &amp; "service_history.begin_on, service_history.window_beg, " qry = qry &amp; "service_history.window_end, service_history.rate, " qry = qry &amp; "service_history.discount, service_history.res_cost, service_history.recurring, service_history.id as schedule_id " qry = qry &amp; "FROM service_history " qry = qry &amp; "INNER JOIN clients ON service_history.client_id = clients.id " qry = qry &amp; "INNER JOIN contacts ON service_history.client_id = contacts.client_id " qry = qry &amp; "AND contacts.primary_contact = 1 " qry = qry &amp; "INNER JOIN assets ON service_history.asset_id = assets.id " qry = qry &amp; "INNER JOIN resource_information ON service_history.resource_id = resource_information.id " qry = qry &amp; "INNER JOIN services ON service_history.service_id = services.id " qry = qry &amp; "WHERE service_history.begin_on = '" &amp; thisDate &amp; "' AND service_history.partner_id = '" &amp; partner_id &amp; "' " qry = qry &amp; "AND service_history.delete_r &lt;&gt; 1 " qry = qry &amp; "ORDER BY res_last_name, d_name" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload