Group join in linq but only where childs is present - linq

I have this simple query and I want to have a list of categories and for each category the number of products inside.
from category in categories
join product in products
on category.Id equals product.Category_Id into productsPerCategories
select new Categories
{
Category = category,
products = productsPerCategories.Count()
};
But if there is no products, I don't want the category to appear. With my current construct, there will be categories with no products inside. How can I achieve that?

Just add a condition
where productsPerCategories.Any()
or
where productsPerCategories.Count() > 0
So your query would be:
from category in categories
join product in products on category.Id equals product.Category_Id into productsPerCategories
where productsPerCategories.Any()
select new Categories
{
Category = category,
products = productsPerCategories.Count()
};

Related

Eloquent query distinct sub id

I have these models
I want to make a query that shows me all the products whose stock quantity> 0 and that does not repeat the products.
My query:
$stock_products_limit = Stock::distinct('product_id')->where('quantity', '!=', 0)->get();
This would be much easier using a size chart relating it to stocks ... but for now I don't have it
I need the model to return me, and then do a foreach:
#foreach($stock_products_limit as $stock_product)
#foreach($stock_product->product->product_images as $i=>$product_image)
...
#endforeach
...
#enforeach
In my models I have the hasMany and belongsTo relations made
How could I make the query? I've been trying the distinct, group by ... but nothing works for me. It only removes the ones with quantity 0 and repeats the product ID ...
Example of the query I want:
SELECT DISTINCT(stocks.product_id)
FROM stocks
INNER JOIN products ON stocks.product_id = products.id
WHERE quantity != 0
ORDER BY product_id
LIMIT 10;
Another example query (but LIMIT doesn't work with IN)
SELECT * from products where id in (SELECT DISTINCT(product_id)
FROM stocks
INNER JOIN products ON stocks.product_id = products.id
WHERE quantity != 0
ORDER BY product_id)
Instead of making the Stock model as the starting point, you might want to use the Product model. Then you don't even have to think about using DISTINCT. Let's use whereHas
return Product::whereHas('stocks', function ($query) {
$query->where('quantity', '>', 0);
})
->limit(10)
->get();

How toc create relations between category and subcategories

I have categories and subcategories tables
Table catgeories:
id
title
description
Table subcategories:
id
category_id
parent_category_id
Data for categories table:
Data for subcategories table
In my case any category has unlimited child categories. How to make relations Many To Many for models to get all categories and there subcategories?
I suggest you to keep the categories in same table like below
id
parent_id
name
1
0
parent cat
2
1
child cat
In the above table parent_id which has 0 value are the actual parent categories, and which has number are child categories, eg: child cate is child of parent cat, in this way you can implement infinite child categories. But Don't worry we can achieve the relations as you have done, please follow as below
Am Assuming categories and subcategories as model. In categories model write a relation as below
public function childCategories(){
return $this->hasMany('\App\subcategories','parent_category_id','id')
->join('categories','categories.id','=','subcategories.categories')
->select("categories.title",'subcategories.*');
}
After the when you calling the categories call it via with as below, you will get categories with related sub categories
$categories = categories::with('childCategories')->get();
To get the category or, the sub-category, you can use this
$allSubCategory = DB::table('sub_categories')->orderBy('id', 'DESC')
->join('categories', 'sub_categories.category_id', '=', 'categories.id')
->select('sub_categories.*', 'categories.category_name')
->get();
$allCategory = DB::table('categorys')->orderBy('id', 'DESC')
->join('sub_categories', 'categorys.category_id', '=', 'sub_categories.id')
->select('categorys.*', 'sub_categories.sub_category_name')
->get();

how to get the count of customers and display them against product?

I am able to get the count of customers against the product but for me it is difficult to display like how i have mentioned in the above image.
You could use a left join and count function:
select p.product,
count(c.name) as cust_count
from product p
left join customer c
on p.product = c.product
group by p.product;
This is assuming you need zero count for the products that don't have any customers.
If that's not the case, you can use simple aggregation:
select product,
count(*) as cust_count
from customer
group by product;

Calculated field returning same value SQL

I have an issue with the below sub-query:
(select AVG(retail)
from STOCK
where category = 'TOYOTA' or category = 'HONDA') as AVERAGE_SALE_PRICE
Entire query:
select
d.name, s.category,(select AVG(retail)
where category = 'TOYOTA' or category = 'HONDA') as AVERAGE_SALE_PRICE
from dealer d join stock s using (dealerID)
The issue is that this calculated field returns the same value for all the rows in the query, I understand that I may have add a GROUP BY but I am quite confused where...
Thanks for any help
Try this query:
select AVG(retail) as AVERAGE_SALE_PRICE, category
from STOCK
where category='TOYOTA' or category='HONDA'
group by category
Update.This should give you the desired results:
select
d.name,
s.category,
(select AVG(s.retail)
from stock s1
where s1.dealerID = s.dealerID
and (s1.category = 'TOYOTA' or s1.category = 'HONDA') as AVERAGE_SALE_PRICE
from dealer d
join stock s using (dealerID)

get all users shopping cart

I'm trying to migrate all user's carts to another system.
Is there a way to obtain every item in the shopping cart for every user?
What I need is only the product and customer IDs
I think when you say shopping cart items you mean order items right??
I'll do this with sql because you need to migrate the info, if you need as collection to use on magento let me know.
SELECT o.customer_id , p.name
FROM sales_flat_quote as o
LEFT JOIN sales_flat_quote_item as i ON i.quote_id = o.entity_id
LEFT JOIN catalog_product_flat_1 as p ON i.product_id = p.entity_id
$list = Mage::getModel('sales/quote')->getCollection();
foreach($list as $li){
$itens = $li->getItemsCollection(); //here we have the itens;
$li->getCustomer()->getName; // here we have the customer name.
foreach($itens as $i){
$i->getName();//itens name;
}
}
You should take a look at quote tables: sales_flat_quote, sales_flat_quote_item and sales_flat_quote_item_option

Resources