We created custom customer attributes for date of birth:
dob_month, dob_day
They are frontend input type of select, and backend type of int. We are trying to get from customers following fields: entity_id, dob, and store_id (if possible one dob field, otherwise, separated into dob_month and dob_day).
Here's were we are struggling, we would like to filter by these option values. Example: dob_month = 05 and dob_day = 08
Everything we have tried we are either having to filter by the value stored in customer_entity_int for example: 1125 = eav_option_value of 05 or we are only able to retrieve the value stored in customer_entity_int.
I figure there must be a way. Any help you can provide would be greatly appreciated.
Here's the closest I have gotten:
$collection = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect('email')
->addAttributeToSelect('dob_month', 'left')
->addAttributeToSelect('dob_day', 'left')
->addExpressionAttributeToSelect('dob', "CONCAT({{dob_month}}, '-',{{dob_day}})", array(
'dob_month', 'dob_day'
))
->addAttributeToFilter('dob_month', array(
array('eq' => '1125')
))
/*->addAttributeToFilter('dob_day', array(
array('eq' => '08')
))*/
->setPageSize(20)
->setCurPage(1);
Note: The PageSize and CurPage is just for testing to limit the results.
In case anyone wants it, the best I got was to do it all via SQL:
SELECT e.entity_id,CONCAT(dob_month_eav.value, '-',dob_day.dob_day) as dob,e.store_id FROM customer_entity AS e
LEFT JOIN customer_entity_int AS cv
ON ( cv.attribute_id = (
SELECT attribute_id FROM eav_attribute
WHERE entity_type_id = e.entity_type_id
AND attribute_code = 'dob_month'
)
AND cv.entity_id = e.entity_id)
LEFT JOIN `eav_attribute_option_value` as `dob_month_eav` ON dob_month_eav.option_id=cv.value
LEFT JOIN (SELECT e.entity_id,dob_day_eav.value as dob_day,e.store_id FROM customer_entity AS e
LEFT JOIN customer_entity_int AS cv
ON ( cv.attribute_id = (
SELECT attribute_id FROM eav_attribute
WHERE entity_type_id = e.entity_type_id
AND attribute_code = 'dob_day'
)
AND cv.entity_id = e.entity_id)
LEFT JOIN `eav_attribute_option_value` as `dob_day_eav` ON dob_day_eav.option_id=cv.value) as dob_day ON dob_day.entity_id=e.entity_id
WHERE dob_month_eav.store_id=0 AND dob_month_eav.value='05' AND dob_day='08'
Related
I am trying to transform a data that has been saved in a table as CLOB to varchar2. When the query is executed the value that is returned is still a CLOB. What can I be doing wrong when it comes to building the query?
I tried
select l.user_id, l.login, l.is_active, l.is_locked,
(SELECT rtrim(xmlagg(xmlelement(e,lav.ATTR_VALUE,',').extract('//text()') order by TO_CHAR(lav.ATTR_VALUE)).getClobVal,',')
FROM LOGIN l1,LOGIN_ATTRIBUTES la,LOGIN_ATTRIBUTE_VALUES lav
WHERE l1.LOGIN_ID = la.LOGIN_ID AND la.ID = lav.LOGIN_ATTRIBUTE_ID AND la.NAME = 'email') as EMAIL
from IAMUSER.LOGIN l
left join IAMUSER.MANAGED_SYS ms on ms.managed_sys_id = l.managed_sys_id
where l.managed_sys_id = 'MOODLE' and login = 'ex392310'
Results:
USER_ID |LOGIN |IS_ACTIVE|IS_LOCKED|EMAIL |
--------------------------------+--------+---------+---------+------+
8a8c9321799d403d01799e401527011c|ex392310|Y |N |[CLOB]|
Change getClobVal() to getStringVal():
select l.user_id,
l.login,
l.is_active,
l.is_locked,
( SELECT rtrim(
xmlagg(
xmlelement(e,lav.ATTR_VALUE,',').extract('//text()')
order by TO_CHAR(lav.ATTR_VALUE)
).getStringVal(),
','
)
FROM LOGIN l1
INNER JOIN LOGIN_ATTRIBUTES la
ON (l1.LOGIN_ID = la.LOGIN_ID)
INNER JOIN LOGIN_ATTRIBUTE_VALUES lav
ON (la.ID = lav.LOGIN_ATTRIBUTE_ID)
WHERE la.NAME = 'email'
) as EMAIL
from IAMUSER.LOGIN l
left join IAMUSER.MANAGED_SYS ms
on ms.managed_sys_id = l.managed_sys_id
where l.managed_sys_id = 'MOODLE'
and login = 'ex392310'
Or, use LISTAGG:
select l.user_id,
l.login,
l.is_active,
l.is_locked,
( SELECT LISTAGG(lav.ATTR_VALUE,',')
WITHIN GROUP (ORDER BY TO_CHAR(lav.ATTR_VALUE))
FROM LOGIN l1
INNER JOIN LOGIN_ATTRIBUTES la
ON (l1.LOGIN_ID = la.LOGIN_ID)
INNER JOIN LOGIN_ATTRIBUTE_VALUES lav
ON (la.ID = lav.LOGIN_ATTRIBUTE_ID)
WHERE la.NAME = 'email'
) as EMAIL
from IAMUSER.LOGIN l
left join IAMUSER.MANAGED_SYS ms
on ms.managed_sys_id = l.managed_sys_id
where l.managed_sys_id = 'MOODLE'
and login = 'ex392310'
Which table couples the orders and the product attributes of the related orders such as size, color and price?
For example, if my order contains a shirt, I need to get the color and size of the shirt through a query.
First you need get the product id from order and fetch super attribute for that product.
$order_id = 10002; // Your order ID;
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllVisibleItems();
foreach($items as $item) {
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = ("SELECT * FROM (
SELECT
ce.sku,
ea.attribute_id,
ea.attribute_code,
CASE ea.backend_type
WHEN 'varchar' THEN ce_varchar.value
WHEN 'int' THEN ce_int.value
WHEN 'text' THEN ce_text.value
WHEN 'decimal' THEN ce_decimal.value
WHEN 'datetime' THEN ce_datetime.value
ELSE ea.backend_type
END AS value,
ea.is_required AS required
FROM catalog_product_entity AS ce
LEFT JOIN eav_attribute AS ea
ON ce.entity_type_id = ea.entity_type_id
LEFT JOIN catalog_product_entity_varchar AS ce_varchar
ON ce.entity_id = ce_varchar.entity_id
AND ea.attribute_id = ce_varchar.attribute_id
AND ea.backend_type = 'varchar'
LEFT JOIN catalog_product_entity_int AS ce_int
ON ce.entity_id = ce_int.entity_id
AND ea.attribute_id = ce_int.attribute_id
AND ea.backend_type = 'int'
LEFT JOIN catalog_product_entity_text AS ce_text
ON ce.entity_id = ce_text.entity_id
AND ea.attribute_id = ce_text.attribute_id
AND ea.backend_type = 'text'
LEFT JOIN catalog_product_entity_decimal AS ce_decimal
ON ce.entity_id = ce_decimal.entity_id
AND ea.attribute_id = ce_decimal.attribute_id
AND ea.backend_type = 'decimal'
LEFT JOIN catalog_product_entity_datetime AS ce_datetime
ON ce.entity_id = ce_datetime.entity_id
AND ea.attribute_id = ce_datetime.attribute_id
AND ea.backend_type = 'datetime'
WHERE ce.entity_id = ".$item->getProductId()."
) AS tab
WHERE tab.value != ''")
$rows = $connection->fetchAll($sql);
print_r($rows);
}
Reference link
I know how to get the product manufacturer id, but I need the product manufacturer id & name using MySQL.
75 is the attribute id for getting product manufacturer id.
Anyone can help?
I want it in SQL format.
First of all I want to get this out of the way.
If you have the product as an object you can get the manufacturer id like this:
$product->getManufacturer();
and for the name:
$product->getAttributeText('manufacturer');
Now for the sql part...I don't know why you would need it but here goes.
Let's say the product id is 10.
Try this query.
SELECT
i.value as manufacturer_id,
v.value as manufacturer_name
FROM
catalog_product_entity_int i
LEFT JOIN
eav_attribute_option o
ON i.value = o.option_id
LEFT JOIN
eav_attribute_option_value v
ON o.option_id = v.option_id AND
v.store_id = 0
WHERE
i.entity_id = 10 AND
i.attribute_id = 75 AND
i.store_id = 0;
if you want the values for an other store view just replace the 0 in v.store_id = 0 and i.store_id = 0 with the value of your store id. 0 means default values.
Great code, thanks!
On my Magento 2.3.1 i have just changed attribute_id to '83' and added SKU. It works correctly.
SELECT
e.sku as SKU,
i.value as manufacturer_id,
v.value as manufacturer_name
FROM
catalog_product_entity_int i
LEFT JOIN
eav_attribute_option o
ON i.value = o.option_id
LEFT JOIN
eav_attribute_option_value v
ON o.option_id = v.option_id AND
v.store_id = 0
LEFT JOIN
catalog_product_entity e
ON i.entity_id = e.entity_id
WHERE
i.attribute_id = 83 AND
i.store_id = 0
ORDER BY `SKU` DESC
i would like to disable products with no images in Magento 1.8. I have tried this code:
UPDATE catalog_product_entity_int SET value = 2
WHERE attribute_id = 4
AND entity_id IN (
SELECT entity_id
FROM catalog_product_entity_media_gallery
RIGHT OUTER JOIN catalog_product_entity ON catalog_product_entity.entity_id = catalog_product_entity_media_gallery.entity_id
WHERE catalog_product_entity_media_gallery.value is NULL
);
but i have this alert:
Column 'entity_id' in field list is ambiguous
How can i resolve?
Thanks!
In your inner query on line 4 you're listing the column entity_id. This column name entity_id is not unique in your sql field list, because the column entity_id is in the catalog_product_entity table and in the catalog_product_entity_media_gallery as well. MySQL simply doesn't know, which one of these two columns should be shown. So you have to prepend the table in your select area:
UPDATE catalog_product_entity_int SET value = 2
WHERE attribute_id = 4
AND entity_id IN (
SELECT `your_table_name`.`entity_id`
FROM catalog_product_entity_media_gallery
RIGHT OUTER JOIN catalog_product_entity ON catalog_product_entity.entity_id = catalog_product_entity_media_gallery.entity_id
WHERE catalog_product_entity_media_gallery.value is NULL
);
I'm using magento 1.7 and I need to get all orders from certain increment_id containing at least one item matching a sku.
Here's what I have:
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status',
array(
'nin' => array(
'new',
'pending',
'pending_payment',
'holded',
'canceled')
))
->addFieldToFilter('increment_id', array('gteq' => $last_order));
If I add a line with:
$orderCollection->addFieldToFilter('sku', $findSku);
I will get a PHP fatal error since 'sku' is not a field. I've tried addAttributeToFilter() and it won't work either.
I know I need to build a join with another table, but I don't know how joins are made in Magento and I don't know which table I should join to.
Any help will be greatly appreciated.
SKUs are placed in order item table not in orders.
Your final query must look like this one:
SELECT o.increment_id
FROM sales_flat_order_item oi
INNER JOIN sales_flat_order o ON o.entity_id = oi.order_id
WHERE product_id=XXX
ORDER BY o.increment_id DESC;
This query can be done using nearly such syntax:
$orderItem = Mage::getModel('sales/order_item')->getCollection();
$orderItem
->getSelect()
->joinInner(array('order' => Mage::getSingleton('core/resource')->getTableName('sales/order')), 'order.entity_id = main_table.order_id' )
->where('product_id=?', $productId)
->order('main_table.order_id DESC');