How can I query RavenDB for all "Products" which are in a list of categories?
Lets say I want all products that their category is "1" or "2" or "3". The list of categories(1,2,3) should by dynamic. it will change based on user input.
If a product can only have one category then it would something like this:
products = from p in session.Query<Product>()
where p.Category.In(new[] { 1,2,3 })
select p;
If multiple categories for a product the following should work:
products = from p in session.Query<Product>()
where p.Categories.Any(new[] { 1,2,3 })
select p;
Related
I have a parent Table "Orders" and child table "Items". They have one to many relationships. I need to do the query using the "Orders" table. I need the Items in the "include". I want to query the order which has a particular Item. But if it also has other items those too should get selected.
A toned-down version of my query looks something like this:
let term = 'phone';
Orders.findAll({
include: [
model: Item,
where: {itemName: { [Op.like]: `%${term}%` }}
]
})
The query happens but if an order has multiple items and if the item name doesn't have the 'term' in it then that particular item gets excluded. Only the item with the term is returned with the order record.
I want if the where condition is true for 1 of the items, all the other items should also get included.
For example, let's say I search for the term 'phone', I want to get the orders which looks something like the below:
Order No: 123ABC
Order Items:
Android Phone
Smart Watch
In my case, the smartwatch doesn't come with the "include" option.
I will be grateful if anyone can help me find a solution.
Thank You.
If this sql can help you..
select *
from Order o
inner join item i on i.orderId = o.id and i.itemName like %term%
inner join item i2 on i2.orderId = o.id
you can use sequelize like this!
let term = 'phone';
Orders.findAll({
include: [
{
model: Item,
as : 'searchItem',
attributes : [],
where: {itemName: { [Op.like]: `%${term}%` }},
{
model: Item,
}
]
})
In need a query that will help me solve this.
Here's my table 'tags':
id (int)
name (String)
user_id (int)
hardware_id (int)
I am grouping the results of the 'tags' table by hardware_id. I also have a List of tags (List<string>).
I want to get the hardware Id of the groups that all of the tags in the custom List matches at a name in the table above.
In other words, I want to get the hardware_id's that the custom List tags matches their name's. There might be name's that doesn't have a match in the custom list, but all of the custom list tags, must be in the group, and if it satisfies this need, I can the Id of that group.
I found it hard to explain and I didn't get an answer for that. I thought about doing it with foreach because it was so hard to solve, but I couldn't do it either and it's very inefficient to do it that way.
Example:
List : ['tag1', 'tag2']
Table Rows:
1, tag1, 5, 1
2, tag2, 5, 1
3, tag3, 5, 1
4, tag4, 5, 2
5, tag5, 6, 2
In this case, I should get the hardware_id of 1, because although one of the hardware Id's have tag3, it doesn't have any rows with a tag name that isn't in the List. IF the List had 'tag4', the hardware_id = 1 WOULDN'T be returned, because the List has a tag that the hardware_id group doesn't have.
If the Group doesn't have an item that the List has, it won't appear in the final result.
Someone game me this code, but it didn't work:
List<decimal> matchingHardareIds = db.tags.GroupBy(x => x.hardware_id)
.Where(x => x.All(s => tags.Contains(s.name.ToLower()) || 0 == tags.Count() && (s.user_id == userId)))
.Select(x => x.Key).ToList();
In that query, when I have one tag in the List and in the table I have several items with hardware_id 1 and one of them has a 'name' that is equal to the value in the List it will return empty results. this is because the rows in the table for a specific group by hardware_id, has a row with a name that doesn't appear in the custom List.
I need a query in either Entity Framework or Linq. Thanks a lot.
Use this:
var t = db.tags.GroupBy(x => x.hardware_Id)
.Where(x => tags.All(y =>
x.Any(z=> z.name == y)))
.Select(x=>x.Key).ToList();
Can not provide you with the entity framework or linq query, but the sql solution is to count the matches like this:
SELECT hardware_id
FROM tags
WHERE name IN (<list>)
GROUP BY hardware_id
HAVING COUNT(DISTINCT name) = <listDistinctCount>
<listDistinctCount> is the count of distinct values in the list. Which you can prepare prior to the query.
I am trying to get the product combinations displayed in Prestashop admin Customer detail view in addition to the products that are displayed for the customer.
This seems to be the relevant call from AdminCustomersController.php in public form renderForm():
$products = $customer->getBoughtProducts();
Then in the Customer class I found the method:
public function getBoughtProducts()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT * FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order
WHERE o.valid = 1 AND o.`id_customer` = '.(int)$this->id);
}
How can I modify this method to show the product combination alongside the product name?
I am using Prestashop version 1.6.0.9.
you can get it using 2 ways:
order_detail table already have field 'product_name' that contains value like 'Product Name - Combination', so you can use $products['product_name'] in that case.
or
if for some reason it is not good for you, same table contains also product_attribute_id field, it is combination id, so:
$combination = new Combination($product['product_attribute_id']);
$attributes = $combination->getAttributesName($id_lang);
var_dump($attributes);
will give you array of attributes that current combination contains.
I am trying to query the Product Manufacturer in Magento 1.7.0.2 . I browse again and again all the table to where I can get the manufacturer table and connect it with product SKU.
I try this query to hope that I can get the manufacturers of the product:
SELECT
eav_attribute_option_value.value FROM
eav_attribute,
eav_attribute_option,
eav_attribute_option_value
WHERE
eav_attribute.attribute_code = 'manufacturer' AND
eav_attribute_option.attribute_id = eav_attribute.attribute_id AND
eav_attribute_option_value.option_id = eav_attribute_option.option_id
but it is not equal to the product manufacturer when I compare the result to my magento admin product manufacturer.
My question is that what should I do to query so that I can get the list of manufacturers of the product so that I can sql join in with catalog_product_enity's SKU.
Does anyone has an idea about my case? I am new with magento so please be gentle with me.
Any help will be appreciated, Thanks in advance
I hope I understood correctly.
If you want to get the manufacturer for a product, you don't need any query.
This should work. Let's assume that you already have the product in var $_product;
$_product->getAttributeText('manufacturer');//this gets you the label
$_product->getManufacturer(); //gets the manufacturer id
[EDIT]
To get this for all the products do this:
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('manufacturer');
$collection->addAttributeToFilter('status', 1);//optional for only enabled products
$collection->addAttributeToFilter('visibility', 4);//optional for products only visible in catalog and search
foreach ($collection as $product) {
$sku = $product->getSku();
$manufacturerId = $product->getManufacturer();
$manufacturerLabel = $product->getAttributeText('manufacturer');
//do something with the values above - like write them in a csv or excel
}
Little late, but this is what I came up with. Works like a charm.
SELECT cpe.sku, cpev.value, cpf1.manufacturer_value
FROM catalog_product_entity cpe
JOIN catalog_product_entity_varchar cpev ON cpev.entity_id = cpe.entity_id
JOIN catalog_product_flat_1 cpf1 ON cpf1.entity_id = cpe.entity_id
WHERE cpev.attribute_id = 191
I am having a lot of difficulty figuring out how to perform a query on 2 tables that don't have reference to each other in code, for example, I have
Customer -> Product* where customer and product have reference to each other
and
Inventory -> Product* where product does not have reference to Inventory
I would like to find all customer who dont have their product in inventory.
I have done this so far
var subQueryInv= DetachedCriteria.For<Inventory>();
subQueryInv
.Add(Restrictions.IsNotNull("Product.Id"))
.SetProjection(Projections.Property<Inventory>(inv=> inv.Product.Id));
var subQueryProd = DetachedCriteria.For<Product>();
subQueryTire
.Add(Subqueries.PropertyNotIn("Id", subQueryInv))
.SetProjection(Projections.Property<Tire>(prod=> prod.Customer.Id));
var subQueryCust= DetachedCriteria.For<Customer>();
subQueryCust
.Add(Subqueries.PropertyIn("Id", subQueryProd))
.SetProjection(Projections.Property<TireSet>(cust => cust.Id));
That works, bt the query is very innefficient, it is generating SQL like this for the Inventory part
...WHERE Product.Id NOT IN (SELECT Inventory.ProductId FROM Inventory WHERE Inventory.ProductId IS NOT NULL)
So for each product record, it is querying the entire Inventory table. How can I get the subquery to have a reference to the parent Id like this:
...WHERE Product.Id NOT IN (SELECT Inventory.ProductId FROM Inventory WHERE Inventory.ProductId IS NOT NULL AND Inventory.ProductId = Product.Id)
?
you can use an alias to reference the main criteria
var subQueryInv= DetachedCriteria.For<Inventory>();
.Add(Restrictions.IsNotNull("Product.Id"))
.Add(Restrictions.PropertyEq("Product", "product"))
.SetProjection(Projections.Property<Inventory>(inv => inv.Product.Id));
var subQueryProd = DetachedCriteria.For<Product>("product");