I have a question, i need do this query in query builder:
SELECT
ps_orders.id_order,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_address.address1,
ps_address.postcode,
ps_address.city,
ps_address.phone_mobile,
ps_orders.id_order,
ps_orders.reference,
ps_order_detail.product_name,
ps_order_detail.product_quantity,
ps_shop.name AS tienda,
ps_carrier.name AS transportista
FROM
ps_customer
INNER JOIN ps_address ON ps_customer.id_customer = ps_address.id_customer
INNER JOIN ps_orders ON ps_address.id_customer =
ps_orders.id_customer AND ps_orders.id_address_delivery =
ps_address.id_address
INNER JOIN ps_order_detail ON ps_orders.id_order =
ps_order_detail.id_order
INNER JOIN ps_shop ON ps_order_detail.id_shop = ps_shop.id_shop
INNER JOIN ps_carrier ON ps_orders.id_carrier = ps_carrier.id_carrier
WHERE
ps_orders.id_order =54
I did this query in query builder:
$naviones=DB::connection('naviones')->table('ps_customer')
->join('ps_address','ps_customer.id_customer','=','ps_address.id_customer')
->join('ps_orders','ps_address.id_customer','=','ps_orders.id_customer')
->join('ps_order_detail','ps_orders.id_order','=','ps_order_detail.id_order')
->join('ps_shop','ps_order_detail.id_shop','=','ps_shop.id_shop')
->join('ps_carrier','ps_orders.id_carrier','=','ps_carrier.id_carrier')
->select('ps_orders.id_address_delivery','ps_orders.id_order','ps_customer.firstname','ps_customer.lastname','ps_customer.email','ps_address.address1',
'ps_address.postcode','ps_address.city','ps_address.phone_mobile','ps_orders.id_order','ps_orders.reference',
'ps_order_detail.product_name','ps_order_detail.product_quantity','ps_shop.name as tienda','ps_carrier.name as transportista')
->where('ps_orders.id_order','=',$idpedido)->get();
My question is: How can i do for made this query section?
INNER JOIN ps_orders ON ps_address.id_customer =
ps_orders.id_customer AND ps_orders.id_address_delivery =
ps_address.id_address
I hope that yours will can help me. Thank you.
You can use join something like this
->join('table2 AS b', function($join){
$join->on('a.field1', '=', 'b.field2')
->where('b.field3', '=', true)
->where('b.field4', '=', '1');
})
in case if you don't want to compare then you can use multiple "on" chained function like below
$query->join('ps_orders', function($join)
{
$join->on('ps_orders.id', '=', 'ps_address.id_customer')
->on('ps_orders.id_address_delivery', '=', 'ps_address.id_address')
});
Related
I have a complicated query that I would like to translate either in Eloquent ORM or with the Query Builder for a Laravel site, but I can't do it, can someone help me?
Here is my SQL query
SELECT opp_id, risk_study.rst_id, risk_study.rst_status
FROM opportunity
LEFT JOIN risk_study ON risk_study.rst_id =
(SELECT risk_study_quote_vehicle.rst_id
FROM risk_study_quote_vehicle
INNER JOIN quote_vehicle ON quote_vehicle.quv_id = risk_study_quote_vehicle.quv_id
INNER JOIN quote ON quote.quo_id = quote_vehicle.quo_id
WHERE quote.opp_id = opportunity.opp_id
ORDER BY risk_study_quote_vehicle.rst_id DESC
LIMIT 0,1)
WHERE 1 = 1
AND opportunity.per_id_process = '5'
AND opportunity.opp_locked = '1'
Here is my solution.
But this query does not retrieve values from the risk_study table.
$opportunities = Opportunity::select('opp_id', 'risk_study.rst_id', 'risk_study.rst_status')
->leftJoin('risk_study', function ($join) {
$join->on('risk_study.rst_id', '=', DB::raw('"SELECT risk_study_quote_vehicle.rst_id FROM risk_study_quote_vehicle INNER JOIN quote_vehicle ON quote_vehicle.quv_id = risk_study_quote_vehicle.quv_id INNER JOIN quote ON quote.quo_id = quote_vehicle.quo_id WHERE quote.opp_id = opportunity.opp_id ORDER BY risk_study_quote_vehicle.rst_id DESC LIMIT 0,1"'));
})
->where([
['per_id_process', 5],
['opp_locked', 1],
])
->get();
Finally, I wrote the query and it works ! Thanks !
$opportunities = Opportunity::query()
->select(
'opp_id',
'risk_study.rst_id',
'risk_study.rst_status'
)
->from('opportunity')
->leftJoin('risk_study', function ($join) {
$join->where('risk_study.rst_id', function ($sub) {
$sub->select('risk_study_quote_vehicle.rst_id')
->from('risk_study_quote_vehicle')
->join('quote_vehicle', 'quote_vehicle.quv_id', 'risk_study_quote_vehicle.quv_id')
->join('quote', 'quote.quo_id', 'quote_vehicle.quo_id')
->whereColumn('quote.opp_id', 'opportunity.opp_id')
->orderByDesc('risk_study_quote_vehicle.rst_id')
->limit(1);
});
})
->where('opportunity.per_id_process', '5')
->where('opportunity.opp_locked', '1')
->get();
You should be able to use a Closure as the second parameter of where() inside your join Closure.
I don't think Laravel's query builder supports the LIMIT 0,1 statement.
I don't know why you need WHERE 1 = 1, but you should be able to use whereRaw for that.
When you're done, the result looks very alike a formatted SQL query.
$opportunities = Opportunity::query()
->select(
'opp_id',
'risk_study.rst_id',
'risk_study.rst_status'
)
->from('opportunity')
->leftJoin('risk_study', function ($join) {
$join->where('risk_study_quote_vehicle.rst_id', function ($sub) {
$sub->select('risk_study_quote_vehicle.rst_id')
->from('risk_study_quote_vehicle')
->join('quote_vehicle', 'quote_vehicle.quv.id', 'risk_study_quote_vehicle.quv_id')
->join('quote', 'quote.quo_id', 'quote_vehicle.quo_id')
->where('quote_opp_id', 'opportunity.opp_id')
->orderByDesc('risk_study_quote_vehicle.rst_id')
->limit(1);
});
})
->whereRaw('1 = 1')
->where('opportunity.per_id_process', '5')
->where('opportunity.opp_locked', '1')
->get();
Hi I want to know how can i do this query in Laravel 8 , I tried adding the join clause but not work as expected, i need join clause? Or maybe there is another form to do it. I search others examples but i donĀ“t see anythat help me. The query is the next:
DB::table('escandallo_p as esc')
->select("esc.material", "esc.referencia", "esc.ancho", "esc.proveedor", "esc.punto",
"esc.precio", "esc.consumo", "esc.veces", "esc.001", "esc.002", "esc.003", "esc.004",
"esc.005", "esc.006", "esc.007", "esc.008", "esc.009", "esc.010", "esc.011", "esc.um", "esc.merma", "esc.importe", "esc.tipo", "esc.xtalla", "esc.fase",
DB::raw("(select anulado from prototipos_p as p where p.prototipo = '".$scandal[0]->prototipo."' and p.tipo = 'c' and p.referencia = esc.referencia )"),
// ignore
//original query "(select anulado from prototipos_p as p where p.prototipo = ",$request->prototipo," and p.tipo = 'c' and p.referencia = esc.referencia ) as 'anulado'",
// "(select clase from prototipos_p as p where p.prototipo = ",$request->prototipo," and p.tipo = 'c' and p.referencia = esc.referencia ) as 'clase'")
//Converted query ->select('pro.anulado')->where('pro.prototipo', $request->prototipo)
// ->where("p.prototipo", "=", $request->prototipo)
->where("esc.id_escandallo", "=", $request->id_escandallo)
->where("esc.id_version", "=", $request->version)
->orderBy("id","asc")
->get();
!!!! I need to pass the esc.referencia to the sub select query
The second select is the conversion of the select inside "" ( i know this is wrong is only for explain it).
Thank you in advance for any suggestion.
Best regards
EDIT: I can solve my problem with DB::raw, but if anyone know others methos are welcome!
You need to pass callback to the join query to add the extra query to the laravel's join method,
Example from Laravel Doc:
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
It is explained in Laravel's doc, Advanced Join Clauses
There is Subquery support too Subquery Joins,
Eg:
$latestPosts = DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)
->groupBy('user_id');
$users = DB::table('users')
->joinSub($latestPosts, 'latest_posts', function ($join) {
$join->on('users.id', '=', 'latest_posts.user_id');
})
->get();
These two might help you to achieve what you are trying
After test joins, joinSub, whereIn and other forms of doing this, I solved my problem using the DB::raw():
DB::table('escandallo_p as esc')
->select('parameters',....,
DB::raw("(SELECT column //(ONLY ONE)
FROM table
WHERE column = '".$parameter."' ...) AS nombre"),
)
->where('column', "=", $parameter)
->orderBy("id","asc")
->get();
I want to join a table based on a couple of constraints, one of which is "where a value is IN an array".
I have this code as part of a complext query builder:
$query->leftJoin('product_variable_values as val_alias, function ($join) use($rule) {
$join->on('products.id', '=', 'val_alias.product_id')
->on('val_alias.product_variable_option_id', 'in', '(1,2,3)');
});
Which outputs something like this:
left join `product_variable_values` as `val_ingredients` on `products`.`id` = `val_ingredients`.`product_id` and `val_ingredients`.`product_variable_option_id` = IN where `products`.`product_id`
You will notice that there is an error in the mysql statement because I use IN as the operator which is not allowed.
Offending code:
->on('val_alias.product_variable_option_id', 'in', '(1,2,3)');:
Offending output:
val_ingredients`.`product_variable_option_id` = IN where `products`.`product_id
What is the correct way to do this?
it same as :
$array = array(1,2,3);
$query->leftJoin('product_variable_values', 'products.id', '=', 'product_variable_values.product_id')
->whereIn('product_variable_values.product_variable_option_id', $array)
I am trying to use laravel query builder like:-
$users = DB::table('baid_collection')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->where(DB::raw("assigned_user_id = '$userId' and DATE(date_entered)=CURDATE()"))
->groupBy('assigned_user_id')
->get();
This query should be like
select sum(total) as total_collect,collection_limit_c from `baid_collections` inner join `users_cstm` on `baid_collections`.`assigned_user_id` = `users_cstm`.`id_c` where assigned_user_id = '15426608-3ea5-f299-7a80-601bd06be2d9' and DATE(date_entered)=CURDATE() group by `assigned_user_id`
But last run query showing me
select sum(total) as total_collect,collection_limit_c from `baid_collection` inner join `users_cstm` on `baid_collections`.`assigned_user_id` = `users_cstm`.`id_c` where assigned_user_id = '15426608-3ea5-f299-7a80-601bd06be2d9' and DATE(date_entered)=CURDATE() is null group by `assigned_user_id`
In query is null giving problem i dont want is null in query
Use whereRaw instead of where like
$users = DB::table('baid_collections')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->whereRaw("assigned_user_id = '{$userId}' and DATE(date_entered)=CURDATE()")
->groupBy('assigned_user_id')
->get();
It's creating a problem due to where needs atleast two parameters and you are just passing one parameter
Check this if it helps, write whereDate instead of add date in raw query.
if you are not using Carbon, you can also use php date("Y-m-d"), method to get date
DB::table('baid_collection')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->where(DB::raw("assigned_user_id = '$userId'"))
->whereDate("date_entered", "=", Carbon::now()->toDateString())
->groupBy('assigned_user_id')->get();
You can try this
$users = DB::table('baid_collection')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->where(DB::raw("assigned_user_id = '$userId' and DATE(date_entered) = '".date('Y-m-d')."'"))
->groupBy('assigned_user_id')
->get();
$records = DB::table('history as a')
->join('reservation2 as b', function($join){
$join->on('a.id', '=', 'b.id')
->on('a.sid', '=', 'b.sid');
})
->whereBetween('a.date', [$sDate, $eDate])
->select('a.*', DB::raw('SUM(a.perday) AS revenue'), 'b.name', 'b.checkin', 'b.checkout', 'b.status')
->groupBy('a.id')
->groupBy('a.sid')
->toSql();
Gives me following query:
select `a`.*, SUM(a.perday) AS revenue, `b`.`name`, `b`.`checkin`, `b`.`checkout`,
`b`.`status`
from `history` as `a`
inner join `reservation2` as `b` on `a`.`id` = `b`.`id` and `a`.`sid` = `b`.`sid`
where `a`.`date` between ? and ? group by `a`.`id`, `a`.`sid`
Many records are displayed if query is passed from mysql command prompt but if I replace ->toSql() with ->get() it does not give me any record.
What is the problem?