Questions tagged subquery

Explore the latest questions and answers asked by our top developers.

So many sub-queries in advanced search "Mysql-Laravel"

I’m building an "Ads" Website based on Laravel as an API where it contains a dynamic-advanced search. Ads can have multiple attributes and the user can have advanced search, but it leads to so many sub-queries if user checked so many attributes to filter by and the query become slow, Any advice about better query […]

Laravel Join Query to get all matching records

i want to get al product attributes (if any) for the given product id using a sub query in join. Below is my query $product_attributes = ProductAttributes::select(‘name as attribute_name’, ‘value as attribute_value’)->get(); $data[‘product’]= Product::select(‘products.*’,’vendors.name as vendor_name’,’product_discounts.start_time’, ‘product_discounts.end_time’, ‘product_discounts.discount_type’,’product_discounts.discount_value’,’product_attributes.name’,’product_attributes.value’) ->leftjoin(‘product_discounts’,’products.id’,’=’,’product_discounts.product_id’) ->joinSub($product_attributes, ‘product_attributes’, function ($join) { $join->on(‘products.id’, ‘=’, ‘product_attributes.product_id’); })->leftjoin(‘vendors’,’products.vendor_id’,’=’,’vendors.id’)->where(‘id’,$request->product_id)->first(); This query gives me following error InvalidArgumentException […]

By stackoverflow account
Published
Categorised as eloquent, join, laravel, sql, subquery Tagged , , , ,

How to do this in Laravel subquery

I have two tables: Receipts (id, name, advice…) and Ingradients (id_ingradient, id_receipt, ingradient, type_measuring …). How can I select data in next order [{ ‘id’: 5, ‘name’: ‘fried potatoes’, ‘advice’: ‘…’, ‘ingradients’: [{ ‘id_ingradient’: 1, ‘id_receipt’: 5, ‘ingradient’: ‘potato’, ‘type_measuring’: ‘gram’ }, { ‘id_ingradient’: 2, ‘id_receipt’: 5, ‘ingradient’: ‘water’, ‘type_measuring’: ‘ml’ }] }] I need […]

By Nazariy Tymoschuk
Published
Categorised as laravel, mysql, php, subquery Tagged , , ,

Laravel Eloquent query between two tables where specific values do not exist

I have two tables, schedule, picks and have different league pages a user can join and create picks from matches listed from schedule table. Once a pick is made I pass that info to picks table(matchid, leagueid, userid, winner). Id like to create a query where instead of displaying all from schedule table in league […]

By OscarL06
Published
Categorised as eloquent, laravel, mysql, php, subquery Tagged , , , ,

Laravel advanced subqueries WHERE CLAUSE based on parent Query

I’m trying to do a query using Eloquent subqueries and facing this problem. So, I have 2 tables, ept_schedules and test_registrants i want to get data of ept_schedules and combine each item with its total participant that have payment_status = 1 and return 0 if none. This is what i want to achieve So following […]

By Andhika Kurniawan
Published
Categorised as eloquent, laravel, subquery Tagged , ,

Laravel/Eloquent: How do I refer to a column of the outer select in a Larvael `->whereNotExists` sub-query?

Assume I have the following simplified table scheme CREATE TABLE albums ( id SEQUENCE PRIMARY KEY, parent_id BIGINT, _lft BIGINT NOT NULL, _rgt BIGINT NOT NULL … ) Albums can be organized as a tree and use the nested set approach. Assume that I have the two integers p_lft and p_rgt (like parent left and […]

By user2690527
Published
Categorised as eloquent, exists, laravel, php, subquery Tagged , , , ,

Transform Query to Eloquent Laravel

How can I convert this Query to Eloquent? select * FROM (select parte, sum(cantidad) from directoscount WHERE deleted_at is null group by parte) t1 LEFT JOIN (select material, sum(totalStock) from lx02 where deleted_at is null group by material) t2 ON t1.parte = t2.material Source: Laravel

By cesgdav
Published
Categorised as eloquent, laravel, mysql, php, subquery Tagged , , , ,

How can We convert raw query to laravel query

I created a SQL script with subquery and join. I need to convert this raw query in laravel. Raw query is : select users.name, users.user_type, count(complaints.id) as total_case, sum(if(complaints.action = ‘Resolved’, 1, 0)) as total_resolved_case, sum(if(complaints.action = ‘Pending’, 1, 0)) as total_pending_case, sum(if(complaints.action = ‘Unrelated’, 1, 0)) as total_unrelated_case, sum(if(complaints.action = ‘Rejected’, 1, 0)) as […]

Laravel Query Builder for SQL subquery WhereIn

I have a SQL query as follows. SELECT * FROM proj WHERE (projname,version) IN (SELECT projname, MAX(version) FROM proj GROUP BY projname ) I would like to convert it to Laravel Query Builder and try somethin like MyDB::whereIn(‘projname’,’version’ function($query) { $query->select(‘projname’,’MAX(version)’) ->from(‘proj’) ->GROUP By(‘projname’); })->get(); Unfortunately, the following error message was given. Can anyone help? […]

By Kevin Shao
Published
Categorised as laravel, mysql, subquery Tagged , ,

How to do this laravel subquery

How can I make this query in Laravel: SELECT * FROM posts LEFT OUTER JOIN (SELECT * from subscribers where subscribers.post_id=2) as sub ON sub.university_id = universities.id Source: Laravel

By Raju Raj
Published
Categorised as laravel, laravel-8, left-join, orm, subquery Tagged , , , ,
1 2 3 4

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq