Where are magento attribute values and product images stored? - magento

I need to create a SQL query to export my Magento site products in XML format.
I can't figure where are attribute values and product images stored.
By now my query is this:
SELECT cpe.entity_id, cpe.sku, csi.qty, eav_color.value, eav_talla.value
FROM catalog_product_entity AS cpe
JOIN cataloginventory_stock_item AS csi ON csi.product_id = cpe.entity_id
JOIN catalog_product_entity_int AS eav_color ON eav_color.entity_id = cpe.entity_id
AND eav_color.attribute_id =85
JOIN catalog_product_entity_int AS eav_talla ON eav_talla.entity_id = cpe.entity_id
AND eav_talla.attribute_id =127
WHERE csi.qty >0
AND csi.is_in_stock
AND cpe.type_id = 'simple'
LIMIT 0 , 30
But I am getting attributes ids (I think). My query returns this:
entity_id sku qty value value
6000 0121011000-RED-L 2.0000 66 5
I am getting 66 as value for column attribute and value 5 for "talla" attribute.. But those values must be "RED" and "L". I don't understand in wich table are those values stored.
And on the other hand I need to get the product images but I can't figure in where table are stored.

Although there is EAV attribute for image it is actually stored in two tables.
The first one is catalog_product_entity_media_gallery with columns:
value_id - id for the current table
attribute_id - id of an attribute in eav_attribute table
entity_id - id of a product from catalog_product_entity table
value - path to the file
The second is catalog_product_entity_media_gallery_value with columns:
value_id - id of the catalog_product_entity_media_gallery row
store_id - id of a store
label - label of an image
position - position in list of images
disabled - disable the image for the store
So catalog_product_entity_media_gallery defines images for products and catalog_product_entity_media_gallery_value handles settings for different store views.

Related

I cannot find certain fields on the MAGENTO database

I was trying to find this fields because i want to obtain all the products and then, make a query but i was not able to do that(they dont appear in the database diagram for Magento 1.9):
sku, code_brand, code_business_unit, code_line, code_group, code_business, min_sale_qty
This is the diagram:
https://anna.voelkl.at/uploads/magento/ce1922.png
I only found the fields "SKU" and "min_sale_qty". What happened to the others?
Do these fields : code_brand, code_business_unit, code_line, code_group, code_business even exist or they were created by someone?
Anyway, i am using MYSQL connected to the MAGENTO database.
I suppose that code_brand, code_business_unit, code_line, code_group, code_business are custom product attributed that you added right?
When you add a custom attribute, Magento add a row in the table eav_attribute.
There we can find attribute_id and backend_type.
For example if code_brand is a integer the backend_type should be int. So in catalog_product_entity_int we can find rows with the same attribute_id set in the eav_attribute table.
Here you are a sumple query to search that:
SELECT
*
FROM
catalog_product_entity_int
WHERE
attribute_id IN (
SELECT
attribute_id
FROM
eav_attribute
WHERE
attribute_code = 'code_brand'
);

magento sql query update category from inactive to is active

I currently have an magento site with a couple of hundred categories, the problem I have is the person who added them set is active to NO. I would like to update all the main and sub categories to is_active to be YES (TRUE).
I have tried the following update Query which updated the is_active to 1 but does not update in Magento even after I re indexed all in Index Management.
UPDATE catalog_category_flat_store_1 SET is_active = 1 WHERE is_active = 0
Thanks for your help.
The catalog_category_flat_store_1 table is a generated table from the EAV tables done for performance reasons. It's not the true source of the category config data.
You'll need to update the data in the EAV. Run this query to find where is_active is stored:
SELECT t1.attribute_id, t1.attribute_code, t1.backend_type
FROM eav_entity_type AS t0
INNER JOIN eav_attribute AS t1 ON (t0.entity_type_id = t1.entity_type_id)
WHERE (t0.entity_model = 'catalog/category')
On my server it has an attribute_id of 42.
You'll then need to query catalog_category_entity and join on catalog_category_entity_int to get the rows to update.

Set Include in Navigation Menu to Yes in database

I want to set Include in Navigation Menu for all categories to Yes.
Can anybody tell me in which table this value is?
Magento use an eav model to save values in the database.
You have to search an attribute called "include_in_menu" in the "eav_attribute" table.
This attribute have an "attribute_id" which will be retrieve in the other tables.
On my installation, this attribute has attribute_id = 67 which is stored as an INTEGER (int)
On magento all the attributes have a type which you can find on the eav_attribute table.
You want to update your categories, we have to update the table where the integer attribute "include_in_menu" of the categories is saved.
As you can see in your database, you have a lot of tables for the categories :
catalog_category_entity, catalog_category_entity_datetime, catalog_category_entity_decimal, catalog_category_entity_int...
You have to select all your categories from the first table : select entity_id from catalog_category_entity
After you have to select the attribute_id in the table related to the good attribute type. Here is "catalog_category_entity_int" for all the INTEGER attribute...
select value from catalog_category_entity_int where attribute_id = 67 and store_id = ...
Be careful if you have multiple stores...All the stores are on the same table.
And now you just have to update value to "1" on the selected rows.
Sorry for my English, i m french...
Best regards
Cédric
This answer worked on Magento EE 2.2.2.
Based on Cedric's answer I used the following query to remove all of the categories from the menu via Sequel Pro.
You can remove all of your categories from your menu by running:
UPDATE catalog_category_entity_int SET value='0' WHERE attribute_id='67'
You can add all of your categories to your menu by running:
UPDATE catalog_category_entity_int SET value='1' WHERE attribute_id='67'
Then flush your cache (php bin/magento cache:flush) and you should see your changes on the frontend.
Again, be careful of what 'store_id' you're affecting.
You can use following query to update all categories:
update catalog_category_entity_int cci
inner join eav_attribute a on a.attribute_id = cci.attribute_id
set cci.value = 1
where a.attribute_code = 'include_in_menu';

Magento disable products with no images via Phpmyadmin

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
);

Retrieving a list of rows with only a single item from other table in Oracle

I have two tables in Oracle database (10g express)
product
product_image
One product can have multiple images. Hence, there a one-to-many relationship from product to product_image and the product_image table has a foreign key that refers to the primary key of the product table.
I need to fetch a list of products with only a single image name in each row of the result set being retrieved regardless of the images being in the product_image table (even though there are no images for some of products).
The image name to be retrieved from the product_image table is generally the first image name in the product_image table after sorting each set of images for each product in ascending order. Something like the following.
prod_id prod_name prod_image
1 aaa aaa.jpg //The first image name in the product_image table after sorting images for prod_id in ascending order.
2 bbb bbb.jpg //Similar to the first case.
3 ccc - //No image(s) found in the product_image table
4 ddd - //Similar to the previous case.
The general join statement for these two tables would be something similar to the following.
SELECT p.prod_id, p.prod_name, pi.prod_image
FROM product p
INNER JOIN product_image pi
ON p.prod_id=pi.prod_id;
Is this possible using a single SQL statement?
If I understood your question correctly I think the following query will work. I have not tested it.
SELECT p.prod_id, p.prod_name, MIN(DBMS_LOB.substr(pi.prod_image, 1))
FROM product p LEFT JOIN product_image pi
ON p.prod_id=pi.prod_id
GROUP BY p.prod_id, p.prod_name
ORDER BY p.prod_name;
Try this, you can generate row number for each image per prod_id and use that. To return null or blank from missing product image you should use left outer join
WITH CTE AS
(
SELECT prod_id, DBMS_LOB.substr(prod_image,1,4000) prod_image,
row_number() over (partition by prod_id order by prod_image) rn
FROM product_image
)
SELECT p.prod_id, p.prod_name, nvl(pi.prod_image,'-') prod_image
FROM product p
LEFT OUTER JOIN CTE pi
ON p.prod_id=pi.prod_id and pi.rn = 1;
The following SQL worked as mentioned in the question.
SELECT
p.prod_id,
p.prod_name,
t.prod_image
FROM
product p
LEFT OUTER JOIN(
SELECT
pi.prod_image_id,
pi.prod_id,
pi.prod_image
FROM
product_image pi
INNER JOIN (
SELECT
MIN(pi.prod_image_id) AS prod_image_id
FROM
product_image pi
GROUP BY
pi.prod_id
) prod_image
ON pi.prod_image_id=prod_image.prod_image_id
)t ON p.prod_id=t.prod_id ORDER BY p.prod_id DESC;
Reference:
SQL Select only rows with Max Value on a Column

Resources